【问题标题】:Android layout external CSSAndroid 布局外部 CSS
【发布时间】:2013-09-04 19:01:56
【问题描述】:

我的 xml 布局(用于 Andoid)中有多个按钮,具有相当的重复性:

        <ImageButton
            android:src="@drawable/tr" //duplicity everywhere but here
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/button1" //and here
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_margin="10dp"
            android:background="@color/button" />

        <ImageButton
            android:src="@drawable/rb"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/button2"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_margin="10dp"
            android:background="@color/button" />

        ...

有没有像 CSS 或一些类/模板这样的东西让我通过将 50dp 更改为 55dp 100 次来度过整个晚上?

如果这很重要,我正在使用 android studio 2.6。

【问题讨论】:

    标签: android css xml layout styles


    【解决方案1】:

    是的。您可以使用主题和样式。 Android 在这里有一个很好的教程:Styles and Themes。要控制所有ImageButtons 的外观和功能,您可以使用android:imageButtonStyle。例如:

    themes.xml

    <style name="MyTheme" parent="@android:style/Theme.DeviceDefault">
        <item name="android:imageButtonStyle">@style/ImageButtonStyle</item>
    </style>
    

    styles.xml

    <style name="ImageButtonStyle" parent="@android:style/Widget.ImageButton">
        <item name="android:layout_width">50dp</item>
        <item name="android:layout_height">50dp</item>
    </style>
    

    这将允许您一次调整所有尺寸。当然,如果您希望只有少数 ImageButtons 使用给定的样式,请删除 android:imageButtonStyle 行,而是对每个 ImageButton 执行以下操作。

    <ImageButton
            android:src="@drawable/tr" //duplicity everywhere but here
            android:id="@+id/button1" //and here
            style="@style/ImageButtonStyle"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_margin="10dp"
            android:background="@color/button" />
    

    【讨论】:

      【解决方案2】:

      您可以(拥有)使用(d)自定义样式(或)引用的属性值。

      如果您这样做了,您可以轻松地更改一个文件中的属性/样式,并查看它反映在使用该文件的所有小部件上。

      您可以参考here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        相关资源
        最近更新 更多