2015-07-21 92 views
1

的标签不补片布局像this选项卡不填充tablayout

我怎样才能让他们充分的标签布局时,每tab == max width/2

主要活动的java:

ViewPager mviewPager = (ViewPager)findViewById(R.id.pager); 
    mviewPager.setAdapter(new viewpager(getSupportFragmentManager(),MainActivity.this)); 

    tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
    tabLayout.setupWithViewPager(mviewPager); 

主要活动的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/Colorprimary" 
    android:elevation="4dp" 
    android:id="@+id/my_tool_bar" 
    /> 

<android.support.design.widget.TabLayout 
    android:id="@+id/sliding_tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="scrollable" 
    /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/pager" 
    android:layout_weight="1" 
    /> 

片段寻呼机apdater的java:

package com.example.smite.floater; 


import android.content.Context; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.widget.Switch; 

public class viewpager extends FragmentPagerAdapter { 

    final int fragments_counter=2; 
    private String titles[] = new String[]{"main","creator"}; 
    private Context context; 

    public viewpager(FragmentManager fm , Context context){ 
     super(fm); 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     return fragments_counter; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 

      case 0: 
       return new main(); 
      case 1: 
       default: 
       return new creator(); 
     } 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     // Generate title based on item position 
     return titles[position]; 
    } 

} 

我的应用程序的主题父母是parent="Theme.AppCompat.Light.NoActionBar"

感谢的帮助(:

+0

您可以使用PagerSlidingTabStrip – kgandroid

+0

如果Google有自己的支持库,请勿使用“PagerSlidingTabStrip”。如有疑问,请咨询:https://github.com/chrisbanes/cheesesquare。 –

+0

如果您删除'app:tabMode =“可滚动”' –

回答

4

如果你把线"app:tabMode="scrollable",那么tablayout宽度将不会填满整个屏幕。最好的解决方案是这样的。 把这一行在XML

app:tabMode="fixed" 

,并在Java中,做

if(tabLayout.getTabCount() > MAX_NUMBER) { 
     tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 
    } 

MAX_NUMBER是根据您的要求,一些号码。我已经使用3,你可以使用3,4或任何适合你的要求。 这将确保如果只有几个选项卡,它们将占用整个宽度,如果不是,则可滚动。

+0

如果此答案对您有帮助,请将其标记为已接受。如果没有,让我更多地了解这个问题。 –

+0

您的答案帮助我解决了不填充标签布局的选项卡的相同问题。所以这意味着如果我把'app:tabMode ='可滚动'和宽度更大,选项卡不会扩展到填充整个宽度。但是如果我把'app:tabMode =“fixed”'他们会。将在这两种情况下扩大。谢谢你的答案 –