在 android 的 tablayout 中添加分隔符

Add separator in tablayout in android

我在我的项目中使用 tablayout,我只有两个选项卡。而且我没有 viewpager 。我想在选项卡之间添加一个分隔符或分隔符,如下所示。

Tab1 | Tab2 

但目前它的显示像

Tab1  Tab2 

我已经检查过 但在这种情况下,他们使用了查看寻呼机。正如我之前所说,我没有 viewpager 。

这是我的 tablayout 代码

xml

<android.support.design.widget.TabLayout
    android:id="@+id/bTabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    android:background="@color/feint_blue"
    app:tabIndicatorHeight="0dp"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/button_text_color"
    app:tabIndicatorColor="@color/color_bottombar_tab_select"
    app:tabTextColor="@color/dark_gray"
    app:textAllCaps="false"
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
    app:tabGravity="fill" /> 

java

        TabLayout bottomTab;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment, container, false);
           bottomTab = (TabLayout) rootView.findViewById(R.id.bTabs);

           bottomTab.addTab(bottomTab.newTab().setText("Tab 1"));
           bottomTab.addTab(bottomTab.newTab().setText("Tab 2"));
        }

从技术上讲,这就是我最终想要的。

我怎样才能做到这一点?

首先为分隔符创建自定义 XML 文件:

tab_layout.xml :

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

    <!-- Assign Tab title in below text view-->

   <TextView
        android:id="@+id/tab_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@drawable/tab_item_selector"/>

     <!-- Create separator -->

     <View
         android:layout_width="1dp"
         android:layout_height="match_parent"
         android:layout_alignParentLeft="true"
         android:background="@android:color/black" />

 </RelativeLayout>

现在在你的 java 文件中代码如下:

  for (int i = 0; i < bottomTab.getTabCount(); i++) {
       TabLayout.Tab tab = bottomTab.getTabAt(i);
       RelativeLayout relativeLayout = (RelativeLayout) 
       LayoutInflater.from(this).inflate(R.layout.tab_layout, bottomTab, false);

       TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.tab_title);
       tabTextView.setText(tab.getText());
       tab.setCustomView(relativeLayout);
       tab.select();
  }

并在 tablayout 标签中添加以下行:

    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"

tab_item_seletor.xml :

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_selected="true" android:color="@color/abc_primary_text_material_dark" />
     <item android:state_focused="true" android:color="@color/abc_primary_text_material_dark" />
     <item android:state_pressed="true" android:color="@color/abc_primary_text_material_dark" />
    <item android:color="@color/abc_secondary_text_material_dark" />
 </selector>