Adjust height between Tablayout title text and icon

我只是一个虾纸丫 提交于 2021-01-02 05:39:12

问题


I there any way to reduce the distance between the title text and the icon of TabLayout like in Google plus where the icons and text title have at least no distance. I have searched , but couldn't find anyway till now.

EDITED

This is how I am setting icon and title:

 tabLayout.getTabAt(3).setIcon(R.drawable.ic_more_horiz_white_24dp);
 tabLayout.getTabAt(3).setText("More");

And this is my TabLayout:

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/white"
        app:tabIndicatorHeight="2dp"
        app:tabTextAppearance="?android:attr/textAppearanceSmall"
        />

回答1:


TabLayout has been introduced to help developers conform to Material Design standards. In that case it is appropriate tab height, padding between icon and text and icon and text size. Look into Material Design Guidelines in order to get familiar with them.

However if you really don't like the padding (and don't want to build application according to Material Design guidelines) you can change it.

You can use @user13 answer. That way You can pass your layout.

However remember that if you would like to build TabLayout more dynamically and use it's TabLayout.Tab#setText(java.lang.CharSequence) and TabLayout.Tab#setIcon(int) you must use layout like that:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/icon"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:id="@android:id/text1"
    android:gravity="center"
    android:layout_below="@android:id/text1" />

Look at the identifiers @android:id/icon and @android:id/text1. If you add those ID's the TabLayout your layout will work with TabLayout class code. Have a look at the documentation for more info.




回答2:


you can try this code, it works for me

for (i in 0 .. tabLayout.tabCount) {
        val params = tabLayout.getTabAt(i)?.view?.getChildAt(0)?.layoutParams as LinearLayout.LayoutParams?
        params?.bottomMargin = 0
        tabLayout.getTabAt(i)?.view?.getChildAt(0)?.layoutParams = params
    }


来源:https://stackoverflow.com/questions/39326469/adjust-height-between-tablayout-title-text-and-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!