Android 的 TableLayout 中每一行的样式都相同

pst*_*cki 5 android android-layout

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>
Run Code Online (Sandbox Code Playgroud)

Kar*_*eek -1

您可以通过在 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>
Run Code Online (Sandbox Code Playgroud)

您可以根据需要命名它,例如res/values/red.xml。然后,您唯一要做的就是在您想要的小部件中使用该视图,例如:

  <?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>
Run Code Online (Sandbox Code Playgroud)

更多参考请点击这里