【问题标题】:Can I define a style that includes a style in Android?我可以在 Android 中定义包含样式的样式吗?
【发布时间】:2015-11-15 20:41:28
【问题描述】:

我创建了以下样式:

<style name="wrap_content">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

我想创建第二种使用这种风格的风格。我知道我可以做到以下几点:

<style name="cycle_radio_button" parent="wrap_content">
    <item name="android:layout_marginStart">1dp</item>
    <item name="android:layout_marginEnd">1dp</item>
</style>

但是有没有办法做类似以下的事情:

<style name="cycle_radio_button">
    <item name="android:layout_marginStart">1dp</item>
    <item name="android:layout_marginEnd">1dp</item>
    <item name="style">@style/wrap_content</item>
</style>

如果可以的话,基于我定义的多个其他样式创建一个样式会更容易、更清晰,而不必创建一系列继承的样式。

【问题讨论】:

    标签: android android-resources android-styles


    【解决方案1】:

    您不能从 Android 中的多个 xml 样式继承。最接近的方法是为特定类型的视图继承样式。例如:

    <style name="style1" parent="Theme.AppCompat.Light">
        <item name="android:listViewStyle">@style/wrap_content</item>
        <item name="android:mapViewStyle">@style/AppTheme</item>
    </style>
    

    现在,任何从“style1”继承的列表视图或地图视图都将继承“wrap_content”中的样式。

    name="android:listViewStyle" 这样的属性只适用于特定的视图类型,所以这不会完全符合你的要求。

    【讨论】:

    • 感谢本斯特罗姆的回答。
    • 很高兴听到这个消息!如果这回答了您的问题,请随时将其标记为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多