问题描述
这是我的标签布局XML
Here is my tab layout XML
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:background="@color/tab_background_primary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/primary_white"
app:tabIndicatorHeight="3dp"
app:tabMinWidth="120dp"
app:tabMode="scrollable"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingTop="1dp"
app:tabPaddingBottom="1dp"
/>
它将删除选项卡之间的水平填充,但不会去除tabPaddingTop和tabPaddingBottom.
It is removing the horizontal padding in between tabs but not the tabPaddingTop and tabPaddingBottom.
如何删除顶部和底部填充,以使每个选项卡都与tabLayout高度匹配?
How do I remove the top and bottom padding to make each tab match the tabLayout height?
每个标签的自定义视图
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab"
android:textColor="@color/primary_white"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="fill"
android:fontFamily="@string/font_fontFamily_medium"/>
我还尝试将线性布局与Imagview和Textview用作自定义视图
I also tried using a Linear Layout with Imagview and Textview as custom view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="fill"
android:layout_height="match_parent">
<ImageView
android:id="@+id/tab_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_settings_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tab_title"
android:textColor="@color/primary_white"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="center"
android:text="test"
android:fontFamily="@string/font_fontFamily_medium"/>
</LinearLayout>
这就是我如何夸大自定义标签视图(对于使用textView的自定义视图)
And here is how I inflated the custom tab view (for custom view with textView)
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("CASH IN");
tabTwo.setBackgroundColor(Color.parseColor("#EC5A0B"));
tabTwo.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_action_cash_light, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
如您所见,我正在尝试为每个标签赋予不同的背景色.但是顶部和底部的填充仍然存在.
As you can see, I am trying to give different background color to each tab. But the padding at top and bottom is still there.
推荐答案
对于此问题,我有最好的解决方案,请检查以下代码,这将有所帮助.
I got the best solution for this issue, check the below code and this will helpful.
我的布局XML是这样的:
My tablayout XML is like this:
<RelativeLayout
android:layout_height="30dp"
android:background="#333333"
android:layout_width="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/MineCustomTabText"
style="@style/tab_bassr"
app:tabMode="scrollable"/>
</RelativeLayout>
与此相对的布局将减少顶部和底部边距
the relative layout upon this will reduce the top and bottom margin
<style name="tab_bassr" parent="TextAppearance.Design.Tab">
<item name="android:layout_width">match_parent</item>
<item name="android:tabStripEnabled">false</item>
<item name="tabPaddingStart">5dp</item>
<item name="tabPaddingEnd">5dp</item>
</style>
<style name="MineCustomTabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
</style>
样式文件用于左右填充和文本大小.
The style file is for left and right padding and the text size.
这篇关于Android TabLayout的tabPaddingTop和tabPaddingBottom未被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!