Android布局外部CSS

标签 android css xml layout styles

我的 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 在此处提供了一个很好的教程: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
        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" />

关于Android布局外部CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560178/

相关文章:

c++ - 使用 C++ 将 HTML 转换为 XML

android - 显示 AutoCompleteTextView 下拉菜单时键盘闪烁

android - ADB 无法识别三星 Galaxy S?

javascript - 网格状、多重响应图像

html - 我可以使用 <span> 来实现吗?

android - 如何在 android 中使用 xmlpullparser 从嵌套的 xml 中提取文本?

javascript - 具有主从关系的数据表ajax xml显示

android - 在 React Native 上设置 Android BroadcastReceiver

android - OpenGL ES 2.0 - 绘制平行线

javascript - 如何从图像中创建下载按钮?