[英]How can I change the color of AlertDialog title and the color of the line under it
[英]Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?
我想在不使用自定義布局的情況下更改AlertDialog
標題顏色和背景顏色。 我的要求,
我試過下面的代碼,但不能工作。
final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
builder.setItems(items, (dialog, item) -> {
});
AlertDialog dialog = builder.create();
dialog.show();
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
tv.setTextColor(activity.getResources().getColor(R.color.white));
tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
使用下面findViewById
行我試過,但它總是在findViewById
返回null
,
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);
我也嘗試使用style
但它只更改標題文本顏色,
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#ffffff</item>
<item name="android:textColor">@color/white</item>
<item name="android:headerBackground">@color/colorPrimary</item>
</style>
您可以在警報對話框中使用自定義標題:
TextView textView = new TextView(context);
textView.setText("Select an option");
textView.setPadding(20, 30, 20, 30);
textView.setTextSize(20F);
textView.setBackgroundColor(Color.CYAN);
textView.setTextColor(Color.WHITE);
final CharSequence[] items = {"Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCustomTitle(textView);
builder.setItems(items, (dialog, item) -> {
}).show();
您可以使用自定義主題更改警報對話框標題、背景和按鈕顏色的顏色。
<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@android:color/black</item>
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
android: windowBackground 改變背景顏色
colorAccent 更改按鈕顏色
android:textColorPrimary 更改對話框標題顏色
將此主題應用於對話框
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialogTheme);
您可以更改警報對話框中的所有內容:
// Title
TextView titleView = new TextView(context);
titleView.setText("Title");
titleView.setGravity(Gravity.CENTER);
titleView.setPadding(20, 20, 20, 20);
titleView.setTextSize(20F);
titleView.setTypeface(Typeface.DEFAULT_BOLD);
titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
titleView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
AlertDialog ad = new AlertDialog.Builder(context).create();
ad.setCustomTitle(titleView);
ad.setCancelable(false);
ad.setMessage("Message");
ad.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// your code
}
});
ad.show();
// Message
TextView messageView = ad.findViewById(android.R.id.message);
if (messageView != null) {
messageView.setGravity(Gravity.CENTER);
}
// Buttons
Button buttonOK = ad.getButton(DialogInterface.BUTTON_POSITIVE);
buttonOK.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
我知道你不想要這個,但使用視圖是最好的方法。 使用自定義視圖,您可以非常輕松地更改每種顏色。
只需創建一個布局並將您的項目放在那里(並更改顏色)
創建一個 LayoutInflater: LayoutInflater layoutinflater = getLayoutInflater;
設置一個視圖,如: View view1 = layoutinflater.inflate(R.layout.yourlayout, null);
將view1
設置為您的構建器: builder.setView(view1);
示例: Textview tx = (TextView)view1.findViewById(R.id.textview)
海事組織你叫dialog.show();
在設置顏色之前嘗試下面的代碼
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
AlertDialog dialog = builder.create();
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
tv.setTextColor(activity.getResources().getColor(R.color.white));
tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
dialog.show(); //change here
更新
只需嘗試將標題設置為警報下方的位置
alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Hello World</font>"));
您可以設置主題:
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
或者您可以添加setOnShowListener()
如下所示:
final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
builder.setItems(items, (dialog, item) -> {
});
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
int titleId = getResources().getIdentifier("alertTitle", "id", "android");
TextView dialogTitle = (TextView) dialog.findViewById(titleId);
dialogTitle.setTextColor(Color.WHITE);
dialogTitle.setBackgroundColor(Color.BLACK);
}
});
dialog.show();
這對我有用,很簡單:由於某種原因,我的對話框顯示的標題背景與對話框背景不同,因此添加此代碼后,問題解決了。
對話框顯示后,用你的顏色設置 setBackgroundDrawable:
mDialog.show();
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(put here your color));
您也可以通過代碼進行更改。
AlertDialog dialog = builder.create();
dialog.show();
Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));
希望能幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.