使用android.support.design.widget.TabLayout显示小选项卡
我正在尝试为每个选项卡创建多个带有片段的选项卡。 我已经做到了,但是两个选项卡出现在布局的角落。 我无法将选项卡布局的宽度平均分配给选项卡。 守则: activity_main.xml使用android.support.design.widget.TabLayout显示小选项卡,android,tabs,Android,Tabs,我正在尝试为每个选项卡创建多个带有片段的选项卡。 我已经做到了,但是两个选项卡出现在布局的角落。 我无法将选项卡布局的宽度平均分配给选项卡。 守则: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.andr
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/MainLayout">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
style = "MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
}您需要将
TabLayout
的tabMode
属性设置为fixed
,而不是scrollable
,这样选项卡将占用所有可用空间,并且根据前面提到的最大选项卡宽度相同。添加app:tabGravity=“fill”
如果要将所有可用空间分配给每个选项卡,请将其添加到您的TabLayout xml中
将TabLayout xml中的app:tabMode=“scrollable”更改为app:tabMode=“fixed”
如果您希望显示所有选项卡
public class Frag_adapter extends FragmentPagerAdapter {
final int PAGE_COUNT =2;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context ccontext;
public Frag_adapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public android.support.v4.app.Fragment getItem(int i)
{
switch (i) {
case 0: return new MyFrag();
case 1: return new MyFrag1();
default: return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}