-
复现一个bug,三个Fragment设置的高度分别是50dp,100dp,150dp,但是最终效果都是全屏,如下。
-
先导入FlycoTabLayout依赖库,因为Demo使用到了SlidingTabLayout,不清楚的可以参考:TabLayout系列:FlycoTabLayout实现点击切换效果。
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
- activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<com.flyco.tablayout.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:background="#ffff00"
app:tl_indicator_color="#4F9AFF"
app:tl_indicator_corner_radius="3dp"
app:tl_indicator_gravity="BOTTOM"
app:tl_indicator_height="4dp"
app:tl_indicator_margin_bottom="8dp"
app:tl_indicator_width="40dp"
app:tl_tab_space_equal="true"
app:tl_textBold="BOTH"
app:tl_textSelectColor="#ff313543"
app:tl_textUnselectColor="#99313543"
app:tl_textsize="15sp" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tabs"
android:background="#ffffff" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java代码
public class MainActivity extends AppCompatActivity {
private Fragment fragment1, fragment2, fragment3;
ViewPager vp;
Fragment[] fragments = new Fragment[3];
String[] nameList = new String[]{"课堂报告","学习记录","其他"};
VPFragmentAdapter adapter;
SlidingTabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = findViewById(R.id.vp);
tabLayout = findViewById(R.id.tabs);
fragment1 = new Test1Fragment();
fragment2 = new Test2Fragment();
fragment3 = new Test3Fragment();
fragments[0]=fragment1;
fragments[1]=fragment2;
fragments[2]=fragment3;
adapter = new VPFragmentAdapter(getSupportFragmentManager(), fragments,nameList);
vp.setAdapter(adapter);
tabLayout.setViewPager(vp,nameList);
}
}
- VPFragmentAdapter代码
public class VPFragmentAdapter extends FragmentPagerAdapter {
private String[] mTitle;
private Fragment[] fragments;
public VPFragmentAdapter(FragmentManager fm, Fragment[] fragments, String[] pageNames) {
super(fm);
this.fragments = fragments;
this.mTitle = pageNames;
}
@Override
public Fragment getItem(int i) {
return fragments[i];
}
@Override
public int getCount() {
return fragments.length;
}
@Override
public CharSequence getPageTitle(int position) {
return mTitle[position];
}
}
- Test1Fragment代码如下,其他Fragment代码类似。
public class Test1Fragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_test, container, false);
return rootView;
}
}
- fragment_test.xml代码如下,另外两个Fragment的布局代码只是颜色、高度不同。
<?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="50dp"
android:background="#00ff00"
tools:context=".Test1Fragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="test1" />
</RelativeLayout>
- 下一篇解决这个bug: ViewPager系列:解决ViewPager内部,Fragment设置高度不起作用(二)