在 Android XML 中有什么方法可以同时为 TableLayout 中的所有行设置相同的样式(或样式参数,如填充)?
我想避免做的是:
<TableLayout>
<TableRow style="@style/MyStyle">...</TableRow>
<TableRow style="@style/MyStyle">...</TableRow>
<TableRow style="@style/MyStyle">...</TableRow>
<TableRow style="@style/MyStyle">...</TableRow>
...
</TableLayout>
最佳答案
您可以通过在 res/values 目录中创建 XML 文件来定义自己的样式。所以,假设您想要红色和粗体文本,然后您创建一个包含以下内容的文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyRedTheme" parent="android:Theme.Light">
<item name="android:textAppearance">@style/MyRedTextAppearance</item>
</style>
<style name="MyRedTextAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
您可以随意命名,例如res/values/red.xml
。然后,您唯一需要做的就是在您想要的小部件中使用该 View ,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="@style/MyRedTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is red, isn't it?"
/>
</LinearLayout>
更多引用click here
关于android - Android TableLayout 中每一行的样式相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30642623/