TabLayout 取消按下时的阴影效果

在TabLayout的xml中添加属性:

 

app:tabBackground="@android:color/transparent"

 

tab 使用自定义的layout 无法填充tab 的宽高问题

 

第一步 为Tablayout 添加tab

private void initTabs() {
        tabWidth = ScreenUtils.getScreenWidth5(this) / 3;
        //遍历tabs,设置每一个tab的样式,默认第一个tab选中
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = tabs.newTab();
            if (tab != null) {
                tab.setCustomView(getTabView(i));
                View view = tab.getCustomView();
                if (i == 0) {
                    setTextColor(view, 0, true);
                }
                tabs.addTab(tab);
            }
        }

        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //在这里可以设置选中状态下  tab字体显示样式
                View view = tab.getCustomView();
                setTextColor(view, tab.getPosition(), true);
                MessageType = tab.getPosition();
                page = 1;
                //请求
                if (MessageType == 0) {
                    getPresenter().getAbnormalMessageList(getAbnormalMessageParam(isAllType), page, false, false);
                }/* else if (MessageType == 1) {
                    getPresenter().getSystemMessageList(getSystemNoticeMessageParam(isAllType), page, false, false);
                } else {
                    getPresenter().getNoticeMessageList(getSystemNoticeMessageParam(isAllType), page, false, false);
                }*/
                adapter.MessageType = MessageType;
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                setTextColor(view, tab.getPosition(), false);
//                setTextColorUnSelect(view, tab.getPosition(), false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageActivity.this.finish();
            }
        });
    }

第二步 为每一个tab 添加layout 布局

/**
     * 设置底部切换tab布局
     *
     * @param curPos
     * @return
     */
    private View getTabView(int curPos) {
        View view = View.inflate(this, R.layout.message_tabs_layout, null);
//        View view = LayoutInflater.from(this).inflate(R.layout.message_tabs_layout, null);
        TextView tv = view.findViewById(R.id.tab_item_tv);
        TextView tv_count = view.findViewById(R.id.tv_count);
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tv.getLayoutParams();
        layoutParams.width = tabWidth;
        tv.setLayoutParams(layoutParams);

        if (mesNum[curPos] == 0) {
            tv_count.setVisibility(View.INVISIBLE);
        } else {
            tv_count.setVisibility(View.VISIBLE);
        }
        tv_count.setText(mesNum[curPos] > 99 ? "99+" : mesNum[curPos] + "");
        tv.setText(titles[curPos]);
        return view;
    }

布局layout  

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/tab_item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="@dimen/dp3"
        android:text="标题"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp1"
        android:background="@drawable/red_circle"
        android:gravity="center"
        android:text="0"
        android:textColor="@color/common_white"
        android:textSize="@dimen/sp8"
        app:layout_constraintLeft_toRightOf="@id/tab_item_tv"
        app:layout_constraintTop_toTopOf="@id/tab_item_tv" />


    <View
        android:id="@+id/view_selected"
        android:layout_width="@dimen/dp40"
        android:layout_height="@dimen/dp10"
        android:layout_marginTop="@dimen/dp2_5"
        android:background="@drawable/purple_rectangle_alpha20_radius5"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@id/tab_item_tv"
        app:layout_constraintLeft_toLeftOf="@id/tab_item_tv"
        app:layout_constraintRight_toRightOf="@id/tab_item_tv" />


</androidx.constraintlayout.widget.ConstraintLayout>


这样就可以将自己定义的布局设置给tab 作为视图使用。但是实际开发中我们发现,tab 中设置的布局无法填充tab 的宽高
首先宽度,宽度无法填充tab 的宽度 是因为tablayout 默认给子view设置了margin 这时候需要你在tablyout 中设置属性来
解决这一问题 

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#fff"
        app:tabBackground="@android:color/transparent"
        app:tabGravity="fill"
        app:tabIndicatorColor="#00000000"
        app:tabMode="fixed"
        app:tabPaddingEnd="-1dp"
        app:tabPaddingStart="-1dp"
        app:tabSelectedTextColor="@color/blue_4158FF"
        app:tabTextColor="@color/gray_666666" />


    app:tabPaddingStart="-1dp" 表示tab的paddingleft 是多少 
    app:tabPaddingEnd="-1dp"   表示tab的paddingright 是多少
 这样设置就可以保证tab 的内容宽度填充。
 其次是自定义视图的高度,其实它在计算的时候 是根据你内部子view 的高度一致,所以你如果想让自己的布局填充高度,
 你需要将你设置的视图 的layout 中的子view 设置一个与其相等的layout_hegit的dp值。如果你在根布局设置这一高度是无效的
 必须是在跟布局中的子view 中设置才能生效.。
 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现在 TabLayout 点击背景取消选中当前选项卡,可以在 TabLayout 的 OnTabSelectedListener 中监听 Tab 的选中状态,当选中状态发生变化,根据需要执行相应的操作。以下是一些实现方法: 1. 在 TabLayout 的 OnTabSelectedListener 中使用 setSelectedTabIndicatorColor 方法,将选中状态下的指示器颜色设置为透明,实现取消选中当前选项卡的效果。 ``` tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { // do something when tab selected } @Override public void onTabUnselected(TabLayout.Tab tab) { // do something when tab unselected } @Override public void onTabReselected(TabLayout.Tab tab) { // do something when tab reselected } }); // set transparent color for selected tab indicator tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT); ``` 2. 在 TabLayout 的 OnTabSelectedListener 中手动设置选中状态,实现取消选中当前选项卡的效果。 ``` tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { // do something when tab selected } @Override public void onTabUnselected(TabLayout.Tab tab) { // manually unselect tab when background clicked tabLayout.getTabAt(tab.getPosition()).select(); } @Override public void onTabReselected(TabLayout.Tab tab) { // do something when tab reselected } }); ``` 注意,第二种方法会在点击任何地方取消选中当前选项卡,而不仅仅是点击 TabLayout 的背景。如果您只想在点击 TabLayout 的背景取消选中当前选项卡,可以使用以下方法: ``` tabLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // unselect tab when background clicked tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).select(); } return false; } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值