首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android TabLayout选项卡图标

Android TabLayout选项卡图标
EN

Stack Overflow用户
提问于 2015-11-10 16:03:43
回答 1查看 1.3K关注 0票数 2

我有以下代码来设置android TabLayout文本和图像:

代码语言:javascript
复制
//First tab - Home Facebook Updates
        tabLayout.addTab(tabLayout.newTab()
                .setText("Tab 1")
                .setIcon(R.drawable.fb_updates));
        //Second tab - Instagram Latest Checkins
        tabLayout.addTab(tabLayout.newTab()
                .setText(Tab 2"));
                .setIcon(R.drawable.ic_location_on_white_24dp));
        //Third tab - Events
    tabLayout.addTab(tabLayout.newTab()
            .setText("אירועים")
            .setIcon(R.drawable.ic_event_note_white_24dp));

代码工作良好,但出于某些原因,选项卡图标出现在选项卡文本的上方,而不是文本附近。例如:

选项卡1图像

表1文本

我需要它这样吸引我:

选项卡1图像标签1文本

EN

回答 1

Stack Overflow用户

发布于 2015-11-10 16:16:35

默认的选项卡布局将把图标放在文本的上方。如果你想把图标放在一边,你必须提供一个定制的布局。

注意:不要给ImageView id android.R.id.icon,因为选项卡布局会自动在它下面放一个边距,不管它在哪里!

做这样的事:

代码语言:javascript
复制
View tabView = LayoutInflater.from(context).inflate(R.layout.custom_tab, null, false);
// use findViewById etc to set the text and images
tabLayout.addTab(
    tabLayout.newTab()
    .setCustomView(tabView )
);

R.layout.custom_tab可以是任何你喜欢的东西,但是很可能是这样的:

代码语言:javascript
复制
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    tools:ignore="UseCompoundDrawables">

    <ImageView
        android:layout_width="@dimen/indicator_size_tab"
        android:layout_height="@dimen/indicator_size_tab"
        android:id="@+id/tab_icon"
        android:layout_marginRight="@dimen/margin_small"
        android:layout_marginEnd="@dimen/margin_small"/>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:id="@android:id/text1"
        android:textAppearance="@style/TextAppearance.Design.Tab"
        android:textColor="@color/tab_text"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"/>

</LinearLayout>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33634323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档