【问题标题】:How do I change the background color of an inherited style in Android layout styles?如何更改 Android 布局样式中继承样式的背景颜色?
【发布时间】:2015-02-06 23:07:10
【问题描述】:

如果我更改子样式的背景,则它不会应用父样式的任何其他样式!

<style name="RemoteButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:textColor">#556699</item>
    <item name="android:textSize">20dp</item>
    <item name="android:layout_margin">3dp</item>
</style>

<style name="ControlButton" parent="RemoteButton">

    <!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
    <item name="android:background">#E87E04</item>

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

这不起作用(被忽略):

<item name="android:colorBackground">#E87E04</item>

如何给子样式设置不同的背景颜色?

【问题讨论】:

    标签: android android-layout android-xml android-styles


    【解决方案1】:

    当您创建自定义样式或主题并从它继承时,请勿使用 parent 属性,而不是此

    <style name="ControlButton" parent="RemoteButton">
    
        <!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
        <item name="android:background">#E87E04</item>
    
        <item name="android:textColor">#ffffff</item>
    </style>
    

    这样做,注意“。”字符。

    <style name="RemoteButton.ControlButton">
    
        <!-- THIS CAUSES IT TO IGNORE ALL THE PARENT STYLES SUCH AS MARGIN -->
        <item name="android:background">#E87E04</item>
    
        <item name="android:textColor">#ffffff</item>
    </style>
    

    你只是继续使用“。”当您需要继承自定义样式时使用字符。

    【讨论】:

    • 我试过了,但仍然没有应用适当的边距!
    • 部分视图不支持边距。
    • 我知道这样做是因为当我使用父样式时它会应用边距
    • 您想在哪个视图上应用此自定义样式?
    • 我正在更改应用程序主要活动中片段的TableRowTableLayout 中的Button 视图
    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2023-03-15
    • 1970-01-01
    • 2018-03-02
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多