Android TabLayout will not equalize immediately after resuming activity

I use TabLayout to move between fragments in the application. I would like the tabs to be aligned to the right of the toolbar (next to the settings icon). And it displays this way when I first open the application:

Desired layout

However, when I rotate the screen to the landscape and then back to the portrait, the tabs are centered on the toolbar:

Unwanted layout

Here is the toolbar code in 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> 

And the onCreate MainActivity.java method

 @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 method :

 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); } 

I have tried many different XML properties, but I am inclined to believe that this is due to Java, due to a problem that occurs when activity is restarted. But I can’t understand why this will happen. Any help is appreciated.

+5
source share
1 answer

This is what I did when I had your problem:

  • started a new project (select tab) with android studio (inspired by their code).
  • this is part of my 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: edit as you wish

0
source

Source: https://habr.com/ru/post/1241648/


All Articles