为啥我的 android 选项卡布局不起作用?
Posted
技术标签:
【中文标题】为啥我的 android 选项卡布局不起作用?【英文标题】:Why is my tab layout for android not working?为什么我的 android 选项卡布局不起作用? 【发布时间】:2019-09-16 13:17:11 【问题描述】:我正在尝试为我的应用创建一个标签滑动活动,但我无法找到问题所在。
这是标签活动的代码:
public class TabActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = findViewById(R.id.container);
viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = findViewById(R.id.tabs);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tab, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
public static abstract class LayoutFragment extends Fragment
private final int layout;
public LayoutFragment(@LayoutRes int layout)
this.layout = layout;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(layout, container, false);
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
public static class Fragment1 extends LayoutFragment
public Fragment1()
super(R.layout.fragment_one);
public static class Fragment2 extends Fragment
public Fragment2()
super(R.layout.fragment_two);
public class SectionsPagerAdapter extends FragmentPagerAdapter
SectionsPagerAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int position)
// getItem is called to instantiate the fragment for the given page.
switch (position)
case 0:
return new Fragment1();
default:
return new Fragment2();
@Override
public int getCount()
return 2;
代码正在完美编译,但片段 1 和片段 2 的选项卡根本没有出现。
我无法弄清楚出了什么问题以及哪里出了问题,我将不胜感激。
编辑:
这是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_
android:layout_
android:background="@drawable/wallpaper"
android:fitsSystemWindows="true"
tools:context=".TabActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_
android:layout_
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem"
android:layout_
android:layout_
android:text="@string/tab1" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem2"
android:layout_
android:layout_
android:text="@string/tab2" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
【问题讨论】:
【参考方案1】:我认为您忘记设置带有标签的视图寻呼机试试这个。
ViewPager viewPager = findViewById(R.id.container);
viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = findViewById(R.id.tabs);
tablayout.setUpWithViewPager(viewPager);
【讨论】:
【参考方案2】:试试这个
tabLayout.setupWithViewPager(viewPager);
并删除这些行
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
编辑
尝试使用LinearLayout
而不是android.support.design.widget.CoordinatorLayout
编辑
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:background="@color/tab_bg"
app:tabGravity="fill"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<View
android:layout_
android:layout_
android:background="@color/colorAccent" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_
android:layout_
android:layout_gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
在活动中这样做
private void init()
ViewPager viewPager = v.findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = v.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
TextView tab1 = (TextView) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_tab, null, false);
TextView tab2 = (TextView) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_tab, null, false);
tabLayout.getTabAt(0).setCustomView(tab1);
tabLayout.getTabAt(1).setCustomView(tab2);
private void setupViewPager(final ViewPager viewPager)
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(viewPagerAdapter);
public class ViewPagerAdapter extends FragmentPagerAdapter
public ViewPagerAdapter(FragmentManager fragmentManager)
super(fragmentManager);
@Override
public Fragment getItem(int position)
if(position == 0) return new Fragment1();
if(position == 1) return new Fragment2();
throw new IllegalStateException("Unexpected position " + position);
@Override
public int getCount()
return 2;
@Override
public CharSequence getPageTitle(int position)
if(position == 0) return "Fragment 1";
if(position == 1) return "Fragment 2";
throw new IllegalStateException("Unexpected position " + position);
这里是custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text1"
android:layout_
android:layout_ />
【讨论】:
还是一样,不过我觉得布局没有问题。以上是关于为啥我的 android 选项卡布局不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 FileProvider 在 android studio 中不起作用
SearchView 在 Android 中不起作用。为啥这样?
为啥我的代码在 android(as3) 中对 CameraRoll 不起作用