我必須在android alertdialog中對齊文本。 但我找不到方法... 任何人都知道如何做到這一點?android:如何在alertDialog中對齊消息?
回答
讓您的TextView填充父項並賦予其中心重力。
<TextView ... android:layout_width="fill_parent" android:gravity="center" />
您將不得不使用在Android中爲AlertDialog提供的構造函數之一,同時創建一個。
AlertDialog(上下文上下文,int主題) 構造使用明確主題的AlertDialog。
這link將幫助你。既然你想讓文本居中,你會想要給出引力屬性,值'中心'。
試試這個
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage("your message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
我知道這個線程是舊的,但可能會幫助一些人:d
TextView title = new TextView(this);
title.setText("Client details not saved!");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
// title.setTextColor(getResources().getColor(R.color.greenBG));
title.setTextSize(23);
TextView msg = new TextView(this);
msg.setText("You're going to lose all the information if you continue!");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
finish();
}
}
};
Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setView(msg);
builder.setCancelable(true);
builder.setPositiveButton("Yes", onClick);
builder.setNegativeButton("No", onClick);
AlertDialog dialog = builder.create();
dialog.show();
請你可以寫出你的代碼是如何有用的並且與其他答案不同。它會幫助人們閱讀你的答案,並決定是否嘗試它(並可能幫助他們學習) – Patrick 2013-03-26 15:49:37
如何在'builder'中添加'title' TextView? – 2013-09-02 03:34:16
試試這個 - 它會做的伎倆。
最好的方法是設計自定義對話框。
view_dialog_box.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A9E2F3">
<TextView
android:id="@+id/txtDiaTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connection Alart"
android:textColor="@color/Black"
android:textStyle="bold"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#2E9AFE"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/txtDiaMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="No Internet Connection"
android:textColor="@color/Black" />
<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OK"
android:textColor="@color/Black"
android:textStyle="bold"
android:padding="5dp"
android:layout_margin="5dp"
android:background="@color/White"/>
然後,它在Java文件中使用
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.view_dialog_box);
// set the custom dialog components - text and button
TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle);
TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg);
Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
您可以使用您的自定義佈局警告對話框佈局。要對齊默認警報對話框佈局信息中心,你可以做
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("hello world");
alertDialog = builder.show();
TextView messageText = (TextView) alertDialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
要小心,如果你設置與findViewById MessageText中打電話之前builder.show(),你會得到一個空指針異常。
只需使用方法和您的對話框的標題和消息將出現在中心:
公共靜態無效openDialog(上下文的背景下,字符串消息){
TextView title = new TextView(context);
// You Can Customise your Title here
title.setText("Information Message");
title.setBackgroundColor(Color.BLACK);
title.setPadding(10, 15, 15, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(22);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCustomTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
// You Can Customise your Message here
TextView messageView = (TextView) alertDialog
.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
- 1. Android無法在AlertDialog上獲取消息對象
- 2. 如何更改AlertDialog消息的顏色?
- 3. 在AlertDialog中右對齊文本
- 4. Tkinter的消息框對齊
- 5. 左右對齊消息
- 6. 如何在QGIS消息欄中對齊QProgressBar標籤?
- 7. 如何取消alertdialog?
- 8. 如何在android中讀取新消息中的消息內容?
- 9. 右對齊AlertDialog中的文本
- 10. 如何在對話框中對齊消息並重用該功能?
- 11. 在Android中對齊
- 12. 在Android中對齊
- 13. Android AlertDialog設置對齊中心的項目
- 14. AlertDialog在android中
- 15. 如何在android中對齊文本
- 16. 如何在Android中對齊按鈕
- 17. 如何在Android Webview中對齊文本?
- 18. 如何在Android中避免此消息
- 19. 如何在Android中恢復消息?
- 20. Android的:如何在AlertDialog
- 21. 如何更改onPrepareDialog()中AlertDialog的消息(正文)?
- 22. 如何使AlertDialog消失?
- 23. 如何在AlertDialog中放置圖像? Android
- 24. 如何在C中對齊控件和工具提示消息的右邊緣#
- 25. 如何在Android中創建子AlertDialog /對話框?
- 26. android - 如何顯示消息
- 27. AlertDialog中性按鈕消除對話框
- 28. 在與多行對齊消息之前使用僞引導警報消息
- 29. Android AlertDialog未顯示單選按鈕或消息
- 30. 更改AlertDialog消息的字體大小
如何做與AlertDialog相同? – 2011-04-04 16:11:58