AlertDialog主题:如何更改项目文本的颜色? [英] AlertDialog Theme: How to change item text color?

查看:523
本文介绍了AlertDialog主题:如何更改项目文本的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将标准主题应用到 AlertDialog

  AlertDialog.Builder建设者=新AlertDialog.Builder(MyClass.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle(更改);

。的String []信息= this.getResources()getStringArray(R.array.info);

ArrayAdapter arrayAdapter =新的ArrayAdapter(这一点,android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(信息);

builder.setSingleChoiceItems(arrayAdapter,...
 

结果:

该说明的是,我有 builder.setItems(...)没有问题,因为它的文字颜色是黑色而施加 builder.setSingleChoiceItems(...)有一个白色的文本颜色。

主题

任何快速解决?或者有什么方法来创建基于 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

自定义主题

果然我的个性风格不工作:

 <样式名称=AlertDialogCustomTheme机器人:父=机器人:Theme.Dialog>
    <项目名称=机器人:文字颜色>#7ABDFF< /项目>
    <项目名称=机器人:windowIsTranslucent>真< /项目>
    <项目名称=机器人:windowBackground> @android:彩色/透明< /项目>


    <! - 以下项目没有影响...! - >

    <项目名称=机器人:layout_centerHorizo​​ntal>真< /项目>
    <项目名称=机器人:layout_centerVertical>真< /项目>
    <项目名称=机器人:textColorAlertDialogListItem>#A844BD< /项目>
    <项目名称=机器人:itemBackground>#7ABDFF< /项目>
< /风格>
 

更新

@lopez答案是一个完整的解决方案,但我发现我的问题一行修复,自定义主题,以应用到活动清单:

 <样式名称=MyTheme的>
    <项目名称=机器人:textColorAlertDialogListItem> @android:彩色/黑白LT; /项目>
< /风格>
 

解决方案

我个人使用an​​droid 对话框但我使用自定义布局相匹配的设计我应用程序。

下面是一个例子:

 新AlertDialog.Builder(上下文)
.setView(inflater.inflate(R.layout.dialog_delete_contact,NULL))
.setPositiveButton(context.getResources()的getString(android.R.string.ok).toUpperCase(),新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话,诠释它){
        //你的治疗
    }
})
.setNegativeButton(context.getResources()的getString(android.R.string.cancel).toUpperCase(),NULL)
。显示();
 

布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/ GrayLight
机器人:方向=垂直>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_gravity =中心
    机器人:背景=@彩色/黑白
    机器人:重力=中心
    机器人:方向=横向
    工具:忽略=DisableBaselineAlignment>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_gravity =中心
        机器人:layout_weight =2
        机器人:重力=中心
        机器人:方向=横向>

        < ImageView的
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:contentDescription =@字符串/ APP_NAME
            机器人:fitsSystemWindows =真
            机器人:填充=10dip
            机器人:SRC =@可绘制/ ic_launcher/>
    < / LinearLayout中>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_gravity =中心
        机器人:layout_marginRight =20dp
        机器人:layout_weight =0.5
        机器人:重力=中心
        机器人:方向=横向>

        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=@字符串/ remove_contact
            机器人:文字颜色=@色/白
            机器人:TEXTSIZE =20SP
            机器人:TEXTSTYLE =黑体
            工具:忽略=硬codedText/>

    < / LinearLayout中>
< / LinearLayout中>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_gravity =中心
    机器人:重力=中心
    机器人:方向=垂直
    工具:忽略=DisableBaselineAlignment>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingBottom会=10dp
        机器人:以下属性来=20dp
        机器人:paddingRight =20dp
        机器人:paddingTop =20dp
        机器人:文本=@字符串/ ask_remove_contact
        机器人:TEXTSIZE =15sp
        工具:忽略=硬codedText/>

< / LinearLayout中>
 

在画面效果:

要避免重写code这里每次都是一个工具类:

 公共类MyDialog {

公共静态生成器创建(最终上下文的背景下,最终LayoutInflater layoutInflater,最后弦乐标题,最后弦乐内容){
    查看查看= layoutInflater.inflate(R.layout.generic_dialog,NULL);
    ((TextView中)view.findViewById(R.id.textViewTitleDialog))的setText(职称)。
    ((TextView中)view.findViewById(R.id.textViewContentDialog))的setText(内容)。
    返回新AlertDialog.Builder(上下文).setView(视图);
}

}
 

和使用的例子:

  AlertDialog.Builder myDialog = MyDialog.create(这一点,getLayoutInflater(),半途而废ECOLEMS,Voulez-VOUS vraiment半途而废L'应用程序?);
myDialog.setPositiveButton(OUI,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话框,INT ID){
        //你的治疗
    }
})
.setNegativeButton(非,NULL)
。显示();
 

我希望你有帮助!

When I try to apply a standard theme to AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....

Result:

The note is that I have no problem with builder.setItems(...) since its text color is Black while the theme applied with builder.setSingleChoiceItems(...) has a White text color.

Any fast fix? Or any way to create a customized theme based on AlertDialog.THEME_DEVICE_DEFAULT_LIGHT ?

My customized style doesn't work as expected:

<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
    <item name="android:textColor">#7ABDFF</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>


    <!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->

    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textColorAlertDialogListItem">#A844BD</item>
    <item name="android:itemBackground">#7ABDFF</item>
</style>

Update

@lopez answer is a complete solution, but I find a single line fix for my problem, a custom theme to be applied to the activity in manifest:

<style name="MyTheme">
    <item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>

解决方案

Personally, I use the android Dialog but I use a custom layout for that match the design of my application.

Here is an example:

new AlertDialog.Builder(context)
.setView(inflater.inflate(R.layout.dialog_delete_contact, null))
.setPositiveButton(context.getResources().getString(android.R.string.ok).toUpperCase(), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // YOUR TREATMENT
    }
})
.setNegativeButton(context.getResources().getString(android.R.string.cancel).toUpperCase(), null)
.show();

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/GrayLight"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/Black"
    android:gravity="center"
    android:orientation="horizontal"
    tools:ignore="DisableBaselineAlignment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true"
            android:padding="10dip"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remove_contact"
            android:textColor="@color/White"
            android:textSize="20sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    tools:ignore="DisableBaselineAlignment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp"
        android:text="@string/ask_remove_contact"
        android:textSize="15sp"
        tools:ignore="HardcodedText" />

</LinearLayout>

Result in picture:

To avoid rewriting the code every time here is a utility class :

public class MyDialog {

public static Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
    View view = layoutInflater.inflate(R.layout.generic_dialog, null);
    ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
    ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
    return new AlertDialog.Builder(context).setView(view);
}

}

And an example of using :

AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), "Quitter ECOLEMS", "Voulez-vous vraiment quitter l'application?");
myDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // YOUR TREATMENT
    }
})
.setNegativeButton("Non", null)
.show();

I hope you have helped!

这篇关于AlertDialog主题:如何更改项目文本的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆