2017-01-04 22 views
1

我有一个tablayout与2个选项卡。我使用的自定义视图设置选项卡Tablayout自定义视图不采取全高android?

Tablayout XML

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     app:tabPaddingBottom="-1dp" 
     app:tabPaddingEnd="-1dp" 
     app:tabPaddingStart="-1dp" 
     app:tabPaddingTop="-1dp" 
     app:tabGravity="fill" 
     app:tabIndicatorHeight="5dp" 
     app:tabMode="fixed" /> 

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

</LinearLayout> 

自定义视图XML

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:andorid="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:textColor="@android:color/white" 
andorid:text="XXX" /> 

Java代码选项卡设置

private void setupTabIcons() { 
    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null); 
    tabOne.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.tabText1)); 
    tabOne.setText(tabTitle[0]); 
    tabLayout.getTabAt(0).setCustomView(tabOne); 

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null); 
    tabTwo.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.tabText2)); 
    tabTwo.setText(tabTitle[1]); 
    tabLayout.getTabAt(1).setCustomView(tabTwo); 
} 

这是我的输出 Tabs not taking full height

如何将textview的高度与tablayout高度相匹配?

+0

把自定义XML的TextView HIGHT 60dp –

+0

集'机器人:layout_height = “WRAP_CONTENT”'在tablayout –

+0

请,如果你想使用参考这只TextView的https://开头的github .com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter –

回答

0

只好以编程方式执行它

ViewGroup.LayoutParams layoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 

设置PARAMS到TextView的

tabOne.setLayoutParams(layoutParams); 
0

,而不是这种尝试这种布局完全访问布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:andorid="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:textColor="@android:color/white" 
     andorid:text="XXX" /> 
</RelativeLayout> 
+0

你明白我的问题了吗? – WISHY

0

尝试使用此..这是我如何自定义我的选项卡

TabLayout tabs = (TabLayout)findviewById(R.id.tabs); 

     ViewGroup vg = (ViewGroup) tabs.getChildAt(0); 
     int tabsCount = vg.getChildCount(); 
     for (int j = 0; j < tabsCount; j++) { 
      ViewGroup vgTab = (ViewGroup) vg.getChildAt(j); 
      int tabChildsCount = vgTab.getChildCount(); 
      for (int i = 0; i < tabChildsCount; i++) { 
       View tabViewChild = vgTab.getChildAt(i); 
       // Get TextView Element 
       if (tabViewChild instanceof TextView) { 
        // change font 
        ((TextView) tabViewChild).setTypeface(tf); 
        // change color 
        ((TextView) tabViewChild).setTextColor(getResources().getColor(R.color.white)); 
        // change size 
        ((TextView) tabViewChild).setTextSize(18); 
        // change padding 
        tabViewChild.setPadding(0, 0, 0, 0); 
        //..... etc... 
       } 
      } 
     } 
0

tablayout WRAP_CONTENT的设置高度并设置TextView的高度60dp/