我正在TabLayout
使用PageViewer
,但Tabs没有填充整个TabLayout。
在xml中,设置GRAVITY_FILL
和MODE_FIXED
是不够的,但设置
android.support.design:tabMaxWidth=0dp
将解决问题。
但是,由于TabLayout.setupWithViewPager(ViewPager)
会覆盖TabLayout
的{{1}},因此我会以编程方式设置标签。
现在TabLayout.Tab没有均匀显示....有人可以帮忙吗?
TabLayout.xml
TabItem
MyActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/activity_main">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
app:tabGravity="fill"
app:tabIndicatorColor="@color/tab_selected_text_color"
app:tabMode="fixed"
app:tabMaxWidth="0dp"> <!--this makes tabitem text filled evenly-->
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_list_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager> <!--not overlapping with appbar-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:elevation="@dimen/shadow"
android:tint="@android:color/background_light"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
app:srcCompat="@android:drawable/ic_input_add"
tools:ignore="UnusedAttribute"
tools:targetApi="lollipop" />
</RelativeLayout>
的gradle:
ViewPager viewPager = (ViewPager) findViewById(R.id.fragment_list_pager);
FragmentPagerAdapter adapter = new ReminderFragmentPagerAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new MyOnPageChangeListener());
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager,true);//needs to be called before addTab, overrides TabTitle
LayoutInflater inflater = LayoutInflater.from(this);
for(int i =0; i<titles.length; i++){
View tabCustomView = inflater.inflate(R.layout.tab_title_layout,null);
tabLayout.addTab(tabLayout.newTab().setCustomView(tabCustomView), i);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
}
目前: enter image description here
更新 这个问题今天早些时候发生了,我搜索了这么多,但没有修复。 刚才......
更改:
在TabLayout中添加TabItem(尽管根据官方网站 他们只是占位符而且没有真正添加;
使用compile "com.android.support:appcompat-v7:26.1.0"
Tab t = TabLayout.getTab;
所以在TabLayout.xml中
t.setCustomView(v);
MyActivity.java中的
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
app:tabGravity="fill"
app:tabIndicatorColor="@color/tab_selected_text_color"
app:tabMode="fixed"
app:tabMaxWidth="0dp"> <!--this makes tabitem text filled evenly-->
<!--TabItem are just view holders and not actually added to TabLayout-->
<!--overridden by viewPagerAdapter.getPageTitle()-->
<android.support.design.widget.TabItem
android:id="@+id/tabItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tab_color_selector"/>
<android.support.design.widget.TabItem
android:id="@+id/tabItemMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tab_color_selector" />
</android.support.design.widget.TabLayout>
这为我解决了问题。谢谢你们!