我在项目中使用tablayout,我只有两个标签。我没有viewpager。我想在下面的标签之间添加一个分隔符或分隔符。
Tab1 | Tab2
但目前显示为
Tab1 Tab2
我已经检查了this,但在这种情况下,他们使用了一个视图寻呼机。正如我之前所说,我没有一个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"));
}
从技术上讲,这是我最终想要的。
我该如何做到这一点?
答案 0 :(得分: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>