【问题标题】:Android AlertDialog custom view text colorAndroid AlertDialog 自定义视图文本颜色
【发布时间】:2013-12-10 19:02:10
【问题描述】:

我想让我的 AlertDialog TextView 颜色(自定义视图)取决于 Android 版本。 在 Android 2.x AlertDialog 有深色背景,在 Android 4.0 有浅色背景。所以文本颜色必须是白色或黑色。 如何引用自动为我制作的样式或主题?

这是我的代码:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.remotedialog, null);
alert.setView(view);

这是我的自定义视图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   
    android:orientation="vertical">

    <TextView
        android:id="@+id/textviewremoteusername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="???"
        android:text="@string/remoteusernametitle" />

    <EditText android:id="@+id/edittextremoteusername"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="textNoSuggestions"
        android:maxLines="1"/>

</LinearLayout>

【问题讨论】:

    标签: android styles textview android-alertdialog


    【解决方案1】:

    好吧,你可以在values-v14/styles 中有一个style 条目作为(盲编码):

    <style name="MyTextStyle">
        <item name="android:textColor">#ffffff</>
    </style>
    

    values/styles:

    <style name="MyTextStyle">
        <item name="android:textColor">#000000</>
    </style>
    

    并从您的布局中参考:

    style="@style/MyTextStyle"
    

    这样,对于 v14 之前的设备,文本颜色将为黑色(您可以自定义颜色),而对于包含 v14 后的设备,文本颜色将为白色。

    【讨论】:

    • 感谢您的快速回答,但没有定义我可以使用的“AlertDialog 文本颜色”的全局样式,例如style="@android:style/AlertDialogTextStyle"?
    • 我没有注意到...如果它存在,我猜它的存在将基于上述机制。
    • 但是您确定 SDK 14 中的所有 AlertDialog 都有白色背景吗?
    • 我不太确定。 API 看起来有浅色背景,但我不保证亚洲制造商的某些模型不会遵守此规则...因此,您应该从样式属性中覆盖尽可能多的内容。
    • @almisoft:到目前为止,我的回答和 cmets 有帮助吗? :)
    【解决方案2】:

    我认为,这是最好的解决方案,它适用于所有 Android 版本、Holo 和 Holo Light 主题:

    res/values/style.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <declare-styleable name="dialog_text_appearance">
            <attr name="alert_dialog_text_appearance" format="reference"/>
        </declare-styleable>
    </resources>
    

    res/values/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="AppThemeABS" parent="Theme.Sherlock">
            <item name="alert_dialog_text_appearance">@style/AlertDialogTextAppearance</item>
        </style>
    
        <style name="AppThemeABSLight" parent="Theme.Sherlock.Light">
            <item name="alert_dialog_text_appearance">@style/AlertDialogTextAppearanceLight</item>
        </style>
    
        <color name="Black">#000000</color>
        <color name="White">#FFFFFF</color>
        <color name="AlertDialogTextColor">#c8c8c8</color>
    
        <style name="AlertDialogTextAppearance">
            <item name="android:textColor">@color/AlertDialogTextColor</item>
        </style>
    
        <style name="AlertDialogTextAppearanceLight">
            <item name="android:textColor">@color/AlertDialogTextColor</item>
        </style>
    
    </resources>
    

    res/values-v11/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="AlertDialogTextAppearance">
            <item name="android:textColor">@color/White</item>
        </style>
    
        <style name="AlertDialogTextAppearanceLight">
            <item name="android:textColor">@color/Black</item>
        </style>
    
    </resources>
    

    res/layout/remotedialog.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"   
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textviewremoteusername"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="?attr/alert_dialog_text_appearance"
            android:text="@string/remoteusernametitle" />
    
        <EditText android:id="@+id/edittextremoteusername"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:inputType="textNoSuggestions"
            android:maxLines="1"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      相关资源
      最近更新 更多