2013-06-21 71 views
12

当我试图将一个标准主题应用到AlertDialogAlertDialog主题:如何更改项目文本颜色?

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, .... 

结果:

enter image description here

的注意的是,我有没有问题builder.setItems(...)因为它的文本颜色为Black,而应用的主题与builder.setSingleChoiceItems(...)有一个白色的文字颜色。

任何快速修复?或者以任何方式创建基于AlertDialog.THEME_DEVICE_DEFAULT_LIGHT的自定义主题?

预期我的个性风格不起作用:

<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> 

更新

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

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

不幸的属性仅适用于API 11及更高版本 – jiduvah

回答

6

个人而言,我使用android Dialog但我使用一个自定义的布局匹配我的应用程序的设计。

下面是一个例子:

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(); 

布局:

<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> 

结果在图:

enter image description here

为了避免这里的每一个时间重写代码是一个工具类:

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); 
} 

} 

而使用的一个例子:

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(); 

我希望你有帮助!

+0

xml看起来不完整 – Manny265

0
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, ....**change this line to** 

builder.setSingleChoiceItems(info,0,null); 
23

如果有人碰巧阅读本,并使用询问服务库警报对话框,并希望改变的列表项的文本颜色则下降了android:第一部分,像这样:

<item name="textColorAlertDialogListItem">@color/your_color</item> 
+2

而且,这用于着色未选中的单选按钮' @ color/your_color' –