【问题标题】:Android Design Tablayout custom view unable to match parentAndroid Design Tablayout自定义视图无法匹配父级
【发布时间】:2016-01-15 22:40:47
【问题描述】:

自定义始终在 Tablayout 中居中。 标签布局如下。 如何获取其父级可用的全部空间,即 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

【问题讨论】:

    标签: android android-design-library android-tablayout


    【解决方案1】:

    Bug 在这里注册: 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2023-01-02
      • 1970-01-01
      相关资源
      最近更新 更多