【问题标题】:how to wrap a single tab width in TabLayout?如何在 TabLayout 中包装单个选项卡宽度?
【发布时间】:2020-06-05 13:50:08
【问题描述】:

参考:https://developer.android.com/reference/com/google/android/material/tabs/TabLayout

TabLayout 提供水平布局来显示选项卡。

要显示的选项卡的填充是通过 TabLayout.Tab 实例完成的。您可以通过 newTab() 创建选项卡。

如何在android标签布局中包裹单个标签,如上图所示。

我也给出了解决方案。如果有人有比这更好的答案,请在这里分享。

【问题讨论】:

    标签: java android xml android-layout android-tablayout


    【解决方案1】:

    创建一个具有 TabLayout => activity_tab_demo.xml 的简单活动 XML

    <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="match_parent"
        android:layout_height="match_parent">
    
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#27C7A1"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    

    创建自定义单个选项卡 => tab1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/dimen_8dp"
        android:paddingEnd="@dimen/dimen_8dp"
        android:layout_gravity="center"
        android:orientation="vertical">
    
        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/ic_camera" />
    
    </LinearLayout>
    
    

    创建活动 TabDemoActivity

    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.carowlers.R;
    import com.google.android.material.tabs.TabLayout;
    
    public class TabDemoActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tab_demo);
            TabLayout tabLayout = findViewById(R.id.tabLayout);
    
            // making custom first tab
            TabLayout.Tab tab1 = tabLayout.newTab();
            tab1.setCustomView(R.layout.tab1);
    
            //setting in tablayout
            tabLayout.addTab(tab1);
            tabLayout.addTab(tabLayout.newTab().setText("Chats"));
            tabLayout.addTab(tabLayout.newTab().setText("Stories"));
            tabLayout.addTab(tabLayout.newTab().setText("Calls"));
            wrapFirstTab(tabLayout);
    
    
        }
    
        public void wrapFirstTab(TabLayout tabLayout) {
            View tabStrip = tabLayout.getChildAt(0);
            if (tabStrip instanceof ViewGroup) {
                ViewGroup tabStripGroup = (ViewGroup) tabStrip;
                View tabView = tabStripGroup.getChildAt(0); // 0th position tab i.e 1st tab
                tabView.setMinimumWidth(0);
                tabView.setPadding(0, tabView.getPaddingTop(), 0, tabView.getPaddingBottom());
                tabView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
                tabLayout.requestLayout();
            }
        }
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-11
      • 2019-07-10
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2018-03-22
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多