在 com.android.support:design:23.1.0 中更改左侧、顶部、右侧或底部的 TabLayout 图标 [英] Changing TabLayout icons on left, top, right or bottom in com.android.support:design:23.1.0

查看:22
本文介绍了在 com.android.support:design:23.1.0 中更改左侧、顶部、右侧或底部的 TabLayout 图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 android 开发还很陌生.所以请耐心等待.

I'm pretty new to android development. So bear with me.

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

I've been trying to align the icon and text in same line in com.android.support:design:23.1.0 for a day.

显然在 com.android.support:design:23.1.0 中,他们已将默认图标位置更改为顶部,并将文本更改为底部.

Apparently in com.android.support:design:23.1.0 they've changed the default icon position to top and text on the bottom.

以前在 com.android.support:design:23.0.1 中默认是左侧的图标和与图标在同一行的文本

Previously in com.android.support:design:23.0.1 the default was the icon on left and text in same line as the icon

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

So here's an easy way to solve it (though it might have drawbacks, idk tbh):

change the version in your app's build.gradle. ex: 23.1.0 to 23.0.1 and build.

还有一个更好的方法来做到这一点(这样你也可以在左、右、上、下对齐图标):

And there's a better way to do it (this way you can also align icons on left,right,top,bottom):

  1. res/layout
  2. 中创建一个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"/>

2.在你的活动java

2. in your activity 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);

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

So far I've achieved to make icons appear on any side like this:

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

PS: setCompoundDrawablesWithIntrinsicBounds function arguments are 4 side icons like this:

setCompoundDrawablesWithIntrinsicBounds(leftDrawable, topDrawable, rightDrawable, bottomDrawable)

推荐答案

感谢 Atu 的这个好提示!

Thank you Atu for this good tip!

就我而言,我必须将线性布局添加到中心 tablayout 标题.我还添加了一些空格字符来获得图标和文本之间的边距.

In my case, I have to add a linear layout to center tablayout title. I have also added some space characters to get margin between the icon and the text.

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>

初始化代码:

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);

这篇关于在 com.android.support:design:23.1.0 中更改左侧、顶部、右侧或底部的 TabLayout 图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆