Android 设计 Tablayout 自定义视图无法匹配父视图
Android Design Tablayout custom view unable to match parent
自定义始终在 Tablayout 中居中。
表格布局如下。
如何让它的父级可用 space,即 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>
但不幸的是它总是居中并且永远不会得到完整的space。问题是我无法在选项卡之间提供分隔线,因为它始终位于中间。
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)
错误已在此处注册:
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);
自定义始终在 Tablayout 中居中。 表格布局如下。 如何让它的父级可用 space,即 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>
但不幸的是它总是居中并且永远不会得到完整的space。问题是我无法在选项卡之间提供分隔线,因为它始终位于中间。
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)
错误已在此处注册: 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);