恢复 Activity 后,Android TabLayout 将不会正确对齐

标签 android android-toolbar

我正在使用 TabLayout 在我的应用程序中的 fragment 之间导航。我希望选项卡对齐到工具栏的右侧(设置图标旁边)。当我第一次打开应用程序时它确实显示如下:

Desired Layout

但是,当我将屏幕旋转到横向然后再旋转回纵向时,选项卡会在工具栏中居中:

Undesired Layout

这是工具栏的代码,在 activity_main.xml 中:

<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.javaonCreate方法

@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 属性,但由于 Activity 重新启动时出现的问题,我倾向于认为这是与 Java 相关的。但我似乎无法找到为什么会发生这种情况。感谢您的帮助。

最佳答案

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

  1. 使用 android studio(受他们的代码启发)开始了新项目(选择选项卡式 Activity )
  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>
    

And this is what i got: 根据需要编辑边距

关于恢复 Activity 后,Android TabLayout 将不会正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045095/

相关文章:

java - Android-TextView 设置十进制值不正确打印逗号

android - 工具栏标题在 fragment 中显示为空

android - 使用 actionLayout 触摸菜单项的反馈

android - NavigationDrawer RecyclerView 选中的项目改变颜色

android - addTextChangedListener 抛出 StackOverflow 错误

java - 如何使用蓝牙从设备获取数据到 Android 应用程序?

android - 在 Android canvas 中绘制 2D 曲线?

android - 如何在工具栏中显示带有文本的图标

android - 如何在android中的工具栏添加 Action 搜索图标

android - 即使添加了 AppBarLayout,工具栏也没有阴影