android TabLayout 相关

1. TabLayout 设置 指示条 颜色、高度,字体颜色大小等

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:background="@color/colorAccent"
    android:layout_gravity="top"
    app:tabIndicatorColor="@color/colorBlue"
    app:tabIndicatorHeight="10dp"
    app:tabSelectedTextColor="@color/colorBlue"
    app:tabTextAppearance="@style/MyTabLayoutText"
    app:tabTextColor="@color/black" />

添加style
<style name="MyTabLayoutText" parent="TextAppearance.Design.Tab">
       
    <item name="android:textSize">40sp</item>
</style>

2.TabLayout 添加tab方法

mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mTabLayout.addTab(mTabLayout.newTab().setText(ResourcesUtil.getString(
    this, R.string.txt_to_send_title)).setTag(TAG_TO_SEND));
mTabLayout.addTab(mTabLayout.newTab().setText(ResourcesUtil.getString(
    this, R.string.txt_wonderful_review)).setTag(TAG_SEND_REVIEW));
mTabLayout.addOnTabSelectedListener(this);

3.TabLayout 设置子item margin值方法

public void setTabItem(@NonNull final TabLayout tabLayout, int marginRight) {
    LogUtil.d(TAG + "setTabItem 000 tabLayout:" + tabLayout);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            try {
                LogUtil.d(TAG + "setTabItem 111");
                //Get the mtabstrip property of tablayout.
                Field tabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
                tabStripField.setAccessible(true);
                LinearLayout tabStrip = (LinearLayout) tabStripField.get(tabLayout);
                int count = tabStrip.getChildCount();
                for (int i = 0; i < count; i++) {
                    View tabView = tabStrip.getChildAt(i);
                    //Get the mtextview property of tabview.
                    Field textViewField = tabView.getClass().getDeclaredField("mTextView");
                    textViewField.setAccessible(true);
                    TextView textView = (TextView) textViewField.get(tabView);
                    tabView.setPadding(0, 0, 0, 0);
                    //Because I want the effect to be as wide as the line is,
                    // measure the width of mtextview.
                    int width = textView.getWidth();
                    if (width == 0) {
                        textView.measure(0, 0);
                        width = textView.getMeasuredWidth();
                    }
                    //Set the space between the left and right of the tab.Note that padding
                    // cannot be used here, because the width of the centerline in the
                    // source code is set according to the width of the tabview
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
                        tabView.getLayoutParams();
                    params.width = width;      //The width of the indicator bar is set here.
                    //params.leftMargin = margin;
                    LogUtil.d(TAG + "setTabItem 222 marginRight:" + marginRight);
                    if (i < count - 1) {
                        params.rightMargin = marginRight;
                    }
                    //params.bottomMargin = marginBottom;
                    tabView.setLayoutParams(params);
                    tabView.invalidate();
                }
            } catch (NoSuchFieldException e) {
                LogUtil.d(TAG + "setTabItem 333 error:" + e);
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                LogUtil.d(TAG + "setTabItem 444 error:" + e);
                e.printStackTrace();
            }
        }
    });
}
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android TabLayout是一个用于显示标签页的控件,可以在布局中使用TabLayout来创建并管理多个标签页。可以使用XML布局文件来定义TabLayout,并设置其属性来自定义样式和行为。 在XML布局中,可以使用TabLayout标签来创建TabLayout控件,并使用TabItem标签来创建每个标签页的布局。可以设置TabItem的属性来定义每个标签页的文本、图标和样式。 如果标签页数量较多且超出屏幕宽度,可以使用属性tabMode="scrollable"来设置TabLayout可滚动,以便所有标签页都可以显示在屏幕上。 以下是一个示例的XML布局代码: ``` <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:tabTextColor="@color/colorPrimary" app:tabSelectedTextColor="@color/colorPrimaryDark" /> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/tab_layout" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这段代码定义了一个包含TabLayout和ViewPager的ConstraintLayout布局。TabLayout用于显示标签页,ViewPager用于显示与标签页对应的内容页。你可以根据自己的需要修改这个布局,并在代码中使用TabLayout和ViewPager来实现TabLayout的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android控件-TabLayout使用介绍](https://blog.csdn.net/csdnxia/article/details/105947804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android控件——Tablayout使用浅析(一)](https://blog.csdn.net/weixin_43499030/article/details/90179867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值