我使用此命令更改了 AlertDialog 标题的颜色
alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"));
但我想改变标题下出现的线条的颜色;我怎样才能做到这一点 ?
注意:我不想使用自定义布局
我使用此命令更改了 AlertDialog 标题的颜色
alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"));
但我想改变标题下出现的线条的颜色;我怎样才能做到这一点 ?
注意:我不想使用自定义布局
不幸的是,这并不是一项特别简单的任务。在我的回答ListSeparator
中,我详细介绍了如何通过检查 Android 使用的父样式、创建新图像以及基于原始样式创建新样式来调整 a 的颜色。不幸的是,与ListSeparator
's 样式不同,AlertDialog
主题是内部的,因此不能作为父样式引用。没有简单的方法可以改变那条小蓝线!因此,您需要求助于制作自定义对话框。
如果这不是你的一杯茶......不要放弃!我很不安,没有简单的方法可以做到这一点,所以我在 github 上建立了一个小项目,用于快速定制全息样式对话框(假设手机支持全息样式)。您可以在这里找到该项目:https ://github.com/danoz73/QustomDialog
它应该可以轻松地从无聊的蓝色变为令人兴奋的橙色!
该项目基本上是使用自定义对话框构建器的示例,在示例中,我创建了一个自定义视图,该视图似乎迎合了您在原始问题中给出的 IP 地址示例。
使用QustomDialog
,为了创建一个带有所需不同颜色的标题或分隔符的基本对话框(标题、消息),您可以使用以下代码:
private String HALLOWEEN_ORANGE = "#FF7F27";
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.");
qustomDialogBuilder.show();
为了添加自定义布局(例如,添加小 IP 地址EditText
),您添加
setCustomView(R.layout.example_ip_address_layout, v.getContext())
使用您设计的布局发送给构建器(IP 示例可以在 github 中找到)。我希望这有帮助。非常感谢 Joseph Earl 和他的回答。
分隔线颜色:
这有点骇人听闻,但对我来说效果很好,并且无需任何外部库即可工作(至少在 Android 4.4 上)。
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
.setIcon(R.drawable.ic)
.setMessage(R.string.dialog_msg);
//The tricky part
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));
您可以在alert_dialog.xml文件中找到更多对话框的 ID 。例如。android:id/alertTitle
用于更改标题颜色...
更新:标题颜色
更改标题颜色的技巧:
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.my_color));
检查这对你有用...
public void setCustomTitle (View customTitleView)
您可以从以下链接获得详细信息。
CustomDialog.java
Dialog alert = new Dialog(this);
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.setContentView(R.layout.title);
TextView msg = (TextView)alert.findViewById(R.id.textView1);
msg.setText("Hello Friends.\nIP address : 111.111.1.111");
alert.show();
标题.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set IP address"
android:textColor="#ff0000"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#00ff00"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#775500"
android:textAppearance="?android:attr/textAppearanceLarge" />
这将为标题、图标和分隔线设置颜色。一定会随着任何新的 Android 版本而改变。
public static void colorAlertDialogTitle(AlertDialog dialog, int color) {
int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
if (dividerId != 0) {
View divider = dialog.findViewById(dividerId);
divider.setBackgroundColor(color);
}
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
if (textViewId != 0) {
TextView tv = (TextView) dialog.findViewById(textViewId);
tv.setTextColor(color);
}
int iconId = dialog.getContext().getResources().getIdentifier("android:id/icon", null, null);
if (iconId != 0) {
ImageView icon = (ImageView) dialog.findViewById(iconId);
icon.setColorFilter(color);
}
}
请记住在调用此方法之前调用 dialog.show()。
跟着Dialog源代码,我发现Title是在Class中MidWindow
通过inflatingdialog_title_holo.xml
布局生成的。所以mTitleView
istitle
的 Id 和 divider 的 Id 是titleDivider
。
title
我们可以通过简单地访问 Id android.R.id.title
。
titleDivider
并通过以下方式访问 IdResources.getSystem().getIdentifier("titleDivider","id", "android");
我用来改变标题方向和改变颜色的最终代码是:
TextView mTitle = (TextView)findViewById(android.R.id.title);
mTitle.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
int x = Resources.getSystem().getIdentifier("titleDivider","id", "android");
View titleDivider = findViewById(x);
titleDivider.setBackgroundColor(getContext().getResources().getColor(R.color.some_color));
如果你不想要一个“图书馆”,你可以使用这个糟糕的黑客:
((ViewGroup)((ViewGroup)getDialog().getWindow().getDecorView()).getChildAt(0)) //ie LinearLayout containing all the dialog (title, titleDivider, content)
.getChildAt(1) // ie the view titleDivider
.setBackgroundColor(getResources().getColor(R.color.yourBeautifulColor));
这已经过测试并在 4.x 上运行;未经测试,但如果我的记忆力很好,它应该适用于 2.x 和 3.x
在 onCreateView 类中,我放了这个:
Dialog d = getDialog();
d.setTitle(Html.fromHtml("<font color='#EC407A'>About</font>"));
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
colorPrimary 链接到存储所有颜色的 colors.xml 文件。还d.setTitle
提供了一种设置标题颜色的 hacky 方法。
这是另一种解决方案(基于建议的答案),它在一个类中处理对话框的样式,而无需担心更改样式时对话框的状态 - 对话框可能已经显示或刚刚初始化。
使用示例:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog = builder.create(); //or builder.show()
DialogViewDecorator.decorate(dialog, android.R.color.holo_red_light); //can also set the defaut color in the class
执行:
public class DialogViewDecorator {
private static final
@ColorRes int DEFAULT_TITLE_DIVIDER_COLOR = android.R.color.holo_orange_light;
public static void decorate(Dialog dialog) {
decorate(dialog, DEFAULT_TITLE_DIVIDER_COLOR);
}
/**
* Sets the title divider color when the view is shown by setting DialogInterface.OnShowListener on the dialog.
* <p/>
* If you want to do other things onShow be sure to extend OnDecoratedDialogShownListener(call super.show(...)!)
* and call {@link #decorate(Dialog, int, OnDecoratedDialogShownListener)}.
*
* @param dialog
* @param titleDividerColor
*/
public static void decorate(Dialog dialog, final int titleDividerColor) {
decorate(dialog, titleDividerColor, new OnDecoratedDialogShownListener(titleDividerColor));
}
/**
* Method for setting a extended implementation of OnDecoratedDialogShownListener. Don't forget to call super
* or the titleDividerColor wont be applied!
*
* @param dialog
* @param titleDividerColor
* @param OnShowListener
* @param <T>
*/
public static <T extends OnDecoratedDialogShownListener> void decorate(Dialog dialog, int titleDividerColor, T OnShowListener) {
if (dialog == null || titleDividerColor <= 0) { return; }
if (dialog.isShowing()) {
setTitleDividerColor(dialog, titleDividerColor);
} else {
dialog.setOnShowListener(OnShowListener);
}
}
private static void setTitleDividerColor(DialogInterface dialogInterface, int titleDividerColor) {
try {
Dialog dialog = (Dialog) dialogInterface;
int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
if (divider != null) {
divider.setBackgroundColor(dialog.getContext().getResources().getColor(titleDividerColor));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static class OnDecoratedDialogShownListener implements DialogInterface.OnShowListener {
private int titleDividerColor;
public OnDecoratedDialogShownListener() {
this.titleDividerColor = DEFAULT_TITLE_DIVIDER_COLOR;
}
public OnDecoratedDialogShownListener(int titleDividerColor) {
this.titleDividerColor = titleDividerColor;
}
@Override
public void onShow(DialogInterface dialogInterface) {
setTitleDividerColor(dialogInterface, titleDividerColor);
}
}}
如果您正在为警报对话框创建自定义布局
那么您可以像这样轻松添加以更改颜色
<LinearLayout
android:id="@+id/DialogTitleBorder"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="@id/mExitDialogDesc"
android:background="#4BBAE3" <!--change color easily -->
>
</LinearLayout>
如果您使用自定义标题布局,那么您可以像这样使用它 alertDialog.setCustomTitle(customTitle);
例子
在 UI 线程上使用对话框,如:
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View customTitle = inflater.inflate(R.layout.customtitlebar, null);
AlertDialog.Builder d = new AlertDialog.Builder(this);
d.setCustomTitle(customTitle);
d.setMessage("Message");
d.setNeutralButton("OK", null);
d.show();
自定义标题栏.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#525f67">
<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" >
</ImageView>
<TextView
android:id="@+id/customtitlebar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#ffffff"
android:text="Title Name"
android:padding="3px"
android:textStyle="bold"
android:layout_toRightOf="@id/icon"
android:layout_alignParentTop="true"
android:gravity="center_vertical"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#ff0000"
android:layout_below="@id/icon"><!-- This is line below the title -->
</ImageView>
</RelativeLayout>
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
String title = context.getString(R.string.agreement_popup_message);
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(title);
ssBuilder.setSpan(
foregroundColorSpan,
0,
title.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context);
alertDialogBuilderUserInput.setTitle(ssBuilder)
从这个答案继续:https://stackoverflow.com/a/15285514/1865860,我从@daniel-smith 分叉了不错的github repo并做了一些改进:
setItems
法items_list
setItems
方法中的禁用项listItem
触摸反馈不要在对话框中使用分隔线,而是在自定义布局中使用视图并将布局设置为对话框中的自定义布局。
custom_popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.divago.view.TextViewMedium
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="AlertDialog"
android:textColor="@android:color/black"
android:textSize="20sp" />
<View
android:id="@+id/border"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/txtTitle"
android:background="@color/txt_dark_grey" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/border"
android:scrollbars="vertical">
<com.divago.view.TextViewRegular
android:id="@+id/txtPopup"
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
活动.java:
public void showPopUp(String title, String text) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.custom_popup, null);
TextView txtContent = alertLayout.findViewById(R.id.txtPopup);
txtContent.setText(text);
TextView txtTitle = alertLayout.findViewById(R.id.txtTitle);
txtTitle.setText(title);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(alertLayout);
alert.setCancelable(true);
alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
如果您使用扩展对话框,请使用:
requestWindowFeature(Window.FEATURE_NO_TITLE);