Android TabLayout在Activity恢复后无法右对齐

5
我正在使用TabLayout在我的应用程序中导航到各个片段。我希望选项卡位于工具栏的右侧(紧挨着设置图标)。当我第一次打开应用程序时,它确实显示为这样:Desired Layout。然而,当我将屏幕旋转到横向,然后再旋转回纵向时,选项卡变成了居中显示在工具栏中:Undesired Layout。以下是activity_main.xml中Toolbar的代码:
<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:paddingTop="16dp"
                android:paddingBottom="16dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_photo_camera_white_24dp"/>

            <TextView
                android:id="@+id/title_text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:paddingLeft="56dp"
                android:paddingStart="56dp"
                android:textColor="@color/white"
              android:textSize="@dimen/abc_text_size_title_material_toolbar"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="right"
                app:tabMode="fixed"
                app:tabMaxWidth="56dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                app:tabIndicatorColor="@android:color/white"/>
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

MainActivity.java中的onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

    TextView tv = (TextView) findViewById(R.id.title_text);
    tv.setText(viewPager.getAdapter().getPageTitle(viewPager.getCurrentItem()));

    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            super.onTabSelected(tab);
            TextView tv = (TextView) findViewById(R.id.title_text);
            tv.setText(tabText[tab.getPosition()]);

        }
    });
}

setupViewPager方法:

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFragment(new HotFragment(), "Hot");
    adapter.addFragment(new YourPostsFragment(), "Your Posts");
    adapter.addFragment(new ExploreMapFragment(), "Explore");
    viewPager.setAdapter(adapter);
}

setupTabIcons方法:

private void setupTabIcons() {
    TabLayout.Tab hot = tabLayout.getTabAt(0);
    TabLayout.Tab your_posts = tabLayout.getTabAt(1);
    TabLayout.Tab explore = tabLayout.getTabAt(2);
    hot.setIcon(tabIcons[0]);
    hot.setText(null);
    your_posts.setIcon(tabIcons[1]);
    your_posts.setText(null);
    explore.setIcon(tabIcons[2]);
    explore.setText(null);
}

我尝试了许多不同的XML属性,但我倾向于认为这与Java有关,因为当活动重新启动时出现了问题。但我似乎找不到原因。感谢任何帮助。


嗨,我只是好奇,在android.support.design.widget.TabLayout中,你使用了android:gravity="right",难道不应该是android:layout_gravity="right"吗? - Sam
1个回答

0

这是我在遇到你的问题时所做的:

  1. 使用Android Studio开始新项目(选择选项卡式活动),并受到他们的代码启发。
  2. 这是我的XML的一部分

                <android.support.design.widget.AppBarLayout
                ...>
    
    <android.support.v7.widget.Toolbar
        ...            >
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            android:layout_gravity="right"
            android:layout_marginRight="24dp"
            style="@style/AppTabLayout"/>
    
    </android.support.v7.widget.Toolbar>
    

这就是我得到的结果: 根据您的意愿编辑右边距


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接