android tablelayout 自定义样式
时间: 2023-06-30 13:04:15 浏览: 57
要自定义 Android TableLayout 的样式,可以使用以下步骤:
1. 创建一个新的 xml 文件,例如 custom_table.xml。
2. 在该文件中定义 TableLayout 的属性,例如背景颜色、边框等等。可以参考 TableLayout 的属性列表来定义。
3. 在该文件中定义 TableRow 的属性,例如背景颜色、边框等等。可以参考 TableRow 的属性列表来定义。
4. 在该文件中定义 TextView 的属性,例如字体颜色、大小、对齐方式等等。可以参考 TextView 的属性列表来定义。
5. 将 TableLayout、TableRow 和 TextView 放置在合适的位置,并按照需要对其进行布局。
以下是一个示例 custom_table.xml 文件:
```xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="10dp"
android:stretchColumns="*">
<TableRow
android:background="#e0e0e0"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp">
<TextView
android:text="Name"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="Age"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="Gender"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:background="#f0f0f0"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp">
<TextView
android:text="John Doe"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="25"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="Male"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:background="#f0f0f0"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp">
<TextView
android:text="Jane Doe"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="30"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="Female"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
```
在代码中使用该自定义 TableLayout 的示例:
```java
// 加载自定义 TableLayout 的布局
LayoutInflater inflater = LayoutInflater.from(this);
View customTable = inflater.inflate(R.layout.custom_table, null);
// 将自定义 TableLayout 添加到主布局中
LinearLayout mainLayout = findViewById(R.id.main_layout);
mainLayout.addView(customTable);
```
以上是自定义 Android TableLayout 样式的基本步骤,可以根据需要进行修改和调整。