简体   繁体   中英

Android Design Tablayout custom view unable to match parent

The custom is always centered in the Tablayout. The tablayout is as below. How to get it the full space available to its parent, ie TabView.

<android.support.design.widget.TabLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"
   android:layout_height="90dp">
</android.support.design.widget.TabLayout>

Adding a textview like below

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/holo_purple"
  android:gravity="center_horizontal">

<TextView
    android:text="Hello !"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

But unfortunately it's always centered and never get the full space. The problem is I can't provide divider between tabs as it's always in center.

Java code for adding tabs:

    TabLayout v = (TabLayout)findViewById(R.id.tablayout);
    TabLayout.Tab tab = v.newTab();
    tab.setCustomView(R.layout.content_main);
    v.addTab(tab);
    tab = v.newTab();
    tab.setCustomView(R.layout.content_main);
    v.addTab(tab);
    tab = v.newTab();
    tab.setCustomView(R.layout.content_main);
    v.addTab(tab)

I'v shared the image @ http://i60.tinypic.com/11cdvyf.jpg

Bug is registered here: https://code.google.com/p/android/issues/detail?id=190429

Although you can workaround it by inflating your custom view and then applying layout params manually:

View v = LayoutInflater.from(this).inflate(R.layout.view_goal_tab_active, null);
TextView tv = (TextView)v.findViewById(R.id.goal_tab_active_tv);
tv.setSelected(true);
v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mTabs.getTabAt(0).setCustomView(v);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM