[英]Android Tab Layout not taking up full width with custom view
Android TabLayout tabPaddingTop 和 tabPaddingBottom 未被刪除
也請參考上面的問題。
即使我將我的設計庫更新為“23.2.0”,標簽布局也一團糟。
下圖是我的選項卡布局。
Xml 部分:-
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="@dimen/dp2"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/white"
app:tabTextAppearance="@style/MyCustomTabTextAppearance" />
樣式 xml :-
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/color_156084</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">@dimen/sp14</item>
<item name="android:textColor">@android:color/white</item>
<item name="textAllCaps">false</item>
</style>
我已將填充設置為 -1dp,甚至還使用 tabGravity 進行填充,但沒有任何效果。
這段代碼曾經在早期版本中工作,但現在如果我降級它,我會在 TintManager 上收到 no class def found 錯誤。
設置不同的值或布局參數不起作用,所以我得到的唯一解決方案是在將選項卡添加到選項卡布局后添加以下內容,
final ViewGroup test = (ViewGroup)(tabs.getChildAt(0));//tabs is your Tablayout
int tabLen = test.getChildCount();
for (int i = 0; i < tabLen; i++) {
View v = test.getChildAt(i);
v.setPadding(0, 0, 0, 0);
}
嘗試將以下屬性添加到 TabLayout:
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
希望它會起作用。
您可以直接訪問TabLayout.Tab
視圖並將填充設置為0
,就像下面的代碼一樣,而不是處理TabLayout
子視圖。
for (int i = 0; i < tabLayout.getTabCount(); i++) {
View tabView = tabLayout.getTabAt(i).view;
tabView.setPadding(0, 0, 0, 0);
}
我正在onCreateView
回調中執行此操作,並且效果很好。
我被困了幾個小時,直到找到方法tab.setCustomView(idRes)
。 從充氣變成這個對我有用。 或者,如果您要充氣,您可以使用TabLayout.TabView
作為根視圖組。
除此之外,我不確定它是否有幫助,但我將minWidth
用於自定義視圖布局。
這就是我想要的
嘗試在 LinearLayout 中添加 TabLayout ,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF" />
</LinearLayout>
在 Styles.xml 添加:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
只需將您的 tabLayout 傳遞給此方法即可完成!
private void setTabLayoutMatchParent(TabLayout tabLayout) {
final ViewGroup tabLayoutChild = (ViewGroup)(tabLayout.getChildAt(0));
int tabLen = tabLayoutChild.getChildCount();
for (int j = 0; j < tabLen; j++) {
View v = tabLayoutChild.getChildAt(j);
v.setPadding(0, 0, 0, 0);
}
}
在我的情況下,問題是為我用於選項卡的自定義布局提供固定高度。 使用match_parent
解決了我的問題。
TabLayout 中有屬性: app:tabPaddingStart和app:tabPaddingEnd
您可以將 -1 設置為它們兩個,以便在 TabLayout 的自定義視圖中刪除填充開始和結束。
<com.google.android.material.tabs.TabLayout
android:id="@+id/tlEmojis"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:tabMaxWidth="@dimen/dimen_48dp"
app:tabMode="scrollable" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.