【发布时间】:2016-02-18 09:39:07
【问题描述】:
我对 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.
还有一个更好的方法来做到这一点(这样你也可以在左、右、上、下对齐图标):
- 在 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"/>
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);
到目前为止,我已经实现了让图标出现在这样的任何一侧:
PS:setCompoundDrawablesWithIntrinsicBounds 函数参数是这样的 4 个侧面图标:
setCompoundDrawablesWithIntrinsicBounds(leftDrawable, topDrawable, rightDrawable, bottomDrawable)
【问题讨论】:
-
访问此androidhive.info/2015/09/… 可能会对您有所帮助
标签: android android-support-library android-tablayout android-support-design