android tab选中变大,Android之解决TabLayout里面每个Tab项的间距和修改指示线的长度(非反射)和修改选中字体大小...
时间: 2024-02-16 08:04:38 浏览: 46
好的,针对您的问题,我分别回答一下:
1. Android中Tab选中变大
您可以通过设置TabLayout的选中Tab的自定义布局,来实现选中Tab变大的效果。具体步骤如下:
1)定义一个自定义的布局文件tab_item.xml,例如:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/tab_text_color" />
<View
android:id="@+id/tab_indicator"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginTop="6dp"
android:background="@color/tab_indicator_color" />
</LinearLayout>
```
2)在TabLayout中设置自定义的布局:
```
TabLayout.Tab tab1 = mTabLayout.newTab().setCustomView(R.layout.tab_item);
TextView tabTitle1 = tab1.getCustomView().findViewById(R.id.tab_title);
tabTitle1.setText("Tab1");
TabLayout.Tab tab2 = mTabLayout.newTab().setCustomView(R.layout.tab_item);
TextView tabTitle2 = tab2.getCustomView().findViewById(R.id.tab_title);
tabTitle2.setText("Tab2");
mTabLayout.addTab(tab1);
mTabLayout.addTab(tab2);
```
3)在TabLayout的OnTabSelectedListener监听器中,实现选中Tab变大的效果:
```
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null) {
TextView tabTitle = customView.findViewById(R.id.tab_title);
View tabIndicator = customView.findViewById(R.id.tab_indicator);
tabTitle.setTextSize(18); // 变大
tabIndicator.setVisibility(View.VISIBLE);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null) {
TextView tabTitle = customView.findViewById(R.id.tab_title);
View tabIndicator = customView.findViewById(R.id.tab_indicator);
tabTitle.setTextSize(16); // 恢复原来的大小
tabIndicator.setVisibility(View.INVISIBLE);
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
```
2. Android之解决TabLayout里面每个Tab项的间距和修改指示线的长度
您可以通过设置TabLayout的属性来实现修改Tab项的间距和指示线的长度。具体步骤如下:
1)修改Tab项的间距:
```
app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp"
```
2)修改指示线的长度:
```
app:tabIndicatorFullWidth="false" // 不占满Tab的宽度
app:tabIndicatorWidth="30dp" // 指示线的宽度
```
3. Android之修改选中字体大小
您可以在OnTabSelectedListener监听器中,实现选中Tab的字体大小变化。具体步骤如下:
```
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null) {
TextView tabTitle = customView.findViewById(R.id.tab_title);
tabTitle.setTextSize(18); // 变大
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View customView = tab.getCustomView();
if (customView != null) {
TextView tabTitle = customView.findViewById(R.id.tab_title);
tabTitle.setTextSize(16); // 恢复原来的大小
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)