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

Atu*_*Atu 22 android android-support-library android-support-design android-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)

小智 23

我有你们想要的确切解决方案。

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:tabInlineLabel="true"
    app:tabPaddingStart="@dimen/default_10dp">
Run Code Online (Sandbox Code Playgroud)

使用以下属性,您可以达到期望的结果。

app:tabInlineLabel =“ true”


que*_*ent 6

感谢Atu提供这个好建议!

在我的情况下,我必须添加一个线性布局到中心tablayout标题.我还添加了一些空格字符来获取图标和文本之间的边距.

custom_tab.xml:

<?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:orientation="vertical"
    android:gravity="center">
    <TextView
        android:id="@+id/tabContent"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:gravity="center"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

初始化代码:

LinearLayout tabLinearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabContent = (TextView) tabLinearLayout.findViewById(R.id.tabContent);
tabContent.setText("  "+getApplicationContext().getResources().getString(tabTitles[i]));
tabContent.setCompoundDrawablesWithIntrinsicBounds(tabIcons[i], 0, 0, 0);
mTabLayout.getTabAt(i).setCustomView(tabContent);
Run Code Online (Sandbox Code Playgroud)