给tablayout的文字设置边框,同时文字与边框之间设置间距
时间: 2023-11-22 08:56:10 浏览: 31
要给TabLayout的文字设置边框,并且文字与边框之间设置间距,可以通过自定义TabLayout的布局实现。
首先,在布局文件中,定义一个新的TextView样式,包含文字边框和间距:
```xml
<style name="TabTextStyle">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/black</item>
<item name="android:paddingStart">12dp</item>
<item name="android:paddingEnd">12dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:background">@drawable/tab_border</item>
</style>
```
其中,@drawable/tab_border是一个Drawable文件,用来定义边框样式。例如:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/black" />
<corners android:radius="20dp" />
</shape>
```
接下来,在TabLayout中使用这个TextView样式:
```xml
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1"
android:layout="@layout/tab_text_layout" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2"
android:layout="@layout/tab_text_layout" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 3"
android:layout="@layout/tab_text_layout" />
</com.google.android.material.tabs.TabLayout>
```
注意,这里的布局文件使用了自定义的tab_text_layout布局,用来显示带有边框和间距的TextView:
```xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/TabTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
这样就可以给TabLayout的文字设置边框,并且文字与边框之间设置间距了。