小编Atu*_*Atu的帖子

在com.android.support:design:23.1.0中更改左侧,顶部,右侧或底部的TabLayout图标

我对Android开发很新.所以忍受我.

我一直在尝试将com.android.support:design:23.1.0中的图标和文本对齐一天.

显然,在com.android.support:design:23.1.0他们已经改变了默认的图标位置,顶部和文字在底部.

以前在com.android.support:design:23.0.1默认在同一行图标左边和文本图标

所以这里有一个简单的方法来解决它(虽然它可能有缺点,idk tbh):

change the version in your app's build.gradle. ex: 23.1.0 to 23.0.1 and build.
Run Code Online (Sandbox Code Playgroud)

还有一种更好的方法(这样你也可以在左,右,上,下对齐图标):

  1. res/layout中创建custom_tab.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"/>
Run Code Online (Sandbox Code Playgroud)

2.在你的活动java中

TextView newTab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
newTab.setText("tab1"); //tab label txt
newTab.setCompoundDrawablesWithIntrinsicBounds(your_drawable_icon_here, 0, 0, 0);
tabLayout.getTabAt(tab_index_here_).setCustomView(newTab);
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经实现了使图标显示在任何一侧,如下所示:

TabLayout图标

PS:setCompoundDrawablesWithIntrinsicBounds函数参数是4个侧面图标,如下所示:

setCompoundDrawablesWithIntrinsicBounds(leftDrawable, topDrawable, rightDrawable, bottomDrawable)
Run Code Online (Sandbox Code Playgroud)

android android-support-library android-support-design android-tablayout

22
推荐指数
2
解决办法
1万
查看次数