更改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
问题描述
我对android开发非常陌生。
I'm pretty new to android development. So bear with me.
我一直在尝试在 com.android.support:design:23.1.0 。
显然在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):
- 在 res / layout 中创建 custom_tab.xml
- create a custom_tab.xml in res/layout
<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屋!