选项卡不会像 this 那样填充选项卡布局
当每个 tab == max width/2
主要 Activity java:
ViewPager mviewPager = (ViewPager)findViewById(R.id.pager);
mviewPager.setAdapter(new viewpager(getSupportFragmentManager(),MainActivity.this));
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(mviewPager);
主要 Activity xml:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Colorprimary"
android:elevation="4dp"
android:id="@+id/my_tool_bar"
/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
android:layout_weight="1"
/>
fragment 寻呼机适配器 java:
package com.example.smite.floater;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Switch;
public class viewpager extends FragmentPagerAdapter {
final int fragments_counter=2;
private String titles[] = new String[]{"main","creator"};
private Context context;
public viewpager(FragmentManager fm , Context context){
super(fm);
this.context = context;
}
@Override
public int getCount() {
return fragments_counter;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new main();
case 1:
default:
return new creator();
}
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return titles[position];
}
}
我的应用主题父级是 parent="Theme.AppCompat.Light.NoActionBar"
谢谢你的帮助(:
最佳答案
如果你把 "app:tabMode="scrollable"
这行,那么 tablayout 宽度将不会填满整个屏幕。最好的解决方案是这样的。
将这一行放在 XML 中
app:tabMode="fixed"
在 Java 中,做
if(tabLayout.getTabCount() > MAX_NUMBER) {
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
MAX_NUMBER 是一些数字,具体取决于您的要求。我用过 3,你可以使用 3,4 或任何适合你要求的东西。 这将确保如果只有几个选项卡,它们将占据整个宽度,如果没有则可以滚动。
关于java - 选项卡不填充 tablayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31538620/