我正在开发一个Android应用程序,我想在其中设置对话框的文本大小。我使用以下代码和XML进行textview,但它只设置我的字段的大小。我也想设置标题的文字大小和颜色。
final CharSequence[] items = { "Fake Request", "Abusive Language","Indecent Approach","Unacceptable Attitude","Dangerous Behavior",};
ArrayAdapter<CharSequence> itemsAdapter = new
ArrayAdapter<CharSequence> (this,
R.layout.menu_items, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setIcon(R.drawable.icon);
builder.setAdapter(itemsAdapter, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item) {
switch(item) {
case 0:
//Mark as Beautiful
break;
case 1:
//Mark as Beautiful
break;
case 2:
//Mark as Not a Portrait
break;
case 3:
//Mark as Offensive
break;
case 4:
//Mark as Spam
break;
case 5:
//cancel
break;
}
}
}
);
builder.show();
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="5dip"
android:gravity="center_vertical"
android:textSize="5dip"
android:textColor="@color/red_theme"
android:typeface="normal"
android:lineSpacingExtra="0dip"/>
答案 0 :(得分:5)
//Use this code
TextView myMsg = new TextView(this);
myMsg.setText("Succesfully send!");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
myMsg.setTextSize(20);
myMsg.setTextColor(Color.WHITE);
//set custom title
builder.setCustomTitle(myMsg);
答案 1 :(得分:3)
您只需要创建自定义对话框,即可控制对话框中的标题,内容和所有内容。请遵循此link
,或遵循此link
,或者只是在google上进行文字研究&#34; android对话框自定义标题&#34;或者使用&#34; android创建自定义对话框&#34;
答案 2 :(得分:3)
在Style.xml中为Dialog创建自定义主题并将其应用于对话框。
示例主题:
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:bottomBright">@color/white</item>
<item name="android:bottomDark">@color/white</item>
<item name="android:bottomMedium">@color/white</item>
<item name="android:textSize">10</item>
<item name="android:centerBright">@color/white</item>
<item name="android:centerDark">@color/white</item>
<item name="android:centerMedium">@color/white</item>
<item name="android:fullBright">@color/orange</item>
<item name="android:fullDark">@color/orange</item>
<item name="android:topBright">@color/blue</item>
<item name="android:topDark">@color/blue</item>
</style>
在你的代码中:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomDialogTheme);
答案 3 :(得分:3)
如果您想使用样式全部完成操作
<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowTitleStyle">@style/DialogTitle</item>
</style>
<style name="DialogTitle" parent="TextAppearance.AppCompat.Large">
<item name="android:textSize">18sp</item>
</style>
并像这样创建对话框
new AlertDialog.Builder(this, R.style.MyDialog);
答案 4 :(得分:2)
使用builder.setCustomTitle(textView)
使用自定义标题设置标题的大小和颜色。
AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView title = new TextView(context);
title.setText("Title");
title.setBackgroundResource(R.drawable.gradient);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(getResources().getColor(R.color.green));
title.setTextSize(22);
builder.setCustomTitle(title);
builder.setMessage("Message");
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(R.string.no, null);
AlertDialog dialog = builder.show();
messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
dialog.show();
答案 5 :(得分:0)
实现这一目标:
执行上述操作即可实现自定义。
您可以使用以下示例:
http://www.mkyong.com/android/android-custom-dialog-example/ http://www.edumobile.org/android/android-development/custom-listview-in-a-dialog-in-android/
我希望它会对你有所帮助。
答案 6 :(得分:0)
这是最简单的方法
首先自定义textView,
TextView dialogTitle = new TextView(getApplicationContext());
dialogTitle.setText("Your Title Text");
dialogTitle.setTextColor(getResources().getColor(R.color.your_color));
dialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
然后写
builder.setCustomTitle(dialogTitle);
而不是
builder.setTitle("My title");
完成!
答案 7 :(得分:0)
有点晚了...但是如果有人遇到同样的问题:
使用 androidx.appcompat.app.AlertDialog
AlertDialog alertdialog;
...
alertdialog.show();
final View v = alertdialog.findViewById(R.id.title_template);
if (v != null) {
v.setBackgroundColor(getResources().getColor(colorPrimaryDark));
TextView title = v.findViewById(R.id.alertTitle);
title.setTextSize(30);
}