安卓设计Tablayout自定义视图无法匹配父布局

8
自定义内容始终位于选项卡布局的中心。 选项卡布局如下所示。 如何使其占用父元素 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>

在下面添加一个文本视图:
 <?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>

很不幸,它总是居中且无法占满整个空间。问题在于我无法提供选项卡之间的分隔符,因为它总是居中。
Java代码添加选项卡:
    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)

我分享了这张图片 @http://i60.tinypic.com/11cdvyf.jpg

1个回答

10

问题的注册页面在这里: https://code.google.com/p/android/issues/detail?id=190429

虽然你可以通过填充自定义视图并手动应用布局参数来解决它:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接