我的 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/