Android:工具栏 Tablayout,在文本上方呈现的图标

标签 android layout graphics tabs android-appcompat

我在对话框中有一个简单的布局,里面有 AppCompat ToolbarTabLayout。我希望我的选项卡在文本左侧有图标。

Here is a layout of the dialog.

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            >

        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"
                app:tabGravity="fill"
                />
    </android.support.v7.widget.Toolbar>

    <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
</LinearLayout>

然后我以编程方式添加选项卡。

tabs.addTab(tabs.newTab()
                .setTag(SettingsTabTag)
                .setText("Settings")
                .setIcon(R.drawable.scan_tab_settings_black), true);
tabs.addTab(tabs.newTab()
                .setTag(MetadataTabTag)
                .setText("Metadata")
                .setIcon(R.drawable.scan_tab_metadata_black));

但是图标总是渲染在文字上方,而且非常小。

最佳答案

1.创建自定义标签布局

<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.在您的 Activity 中设置自定义标签

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

setCompoundDrawablesWithIntrinsicBounds做的工作。

关于Android:工具栏 Tablayout,在文本上方呈现的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177700/

相关文章:

使用 fragment 和 inflater 将布局膨胀为 fragment 时,Android 布局会重叠

html - UL 的 - 如何在顶部添加标题?

改变 RGB 颜色的亮度

matlab - 在 MATLAB 中使用 'fill' 函数时如何更改边线颜色?

c++ - 为什么我的 OpenGL 纹理显示为白色?

android - 简单隐藏/混淆 APK 中的字符串?

android - Intent 打开谷歌身份验证器

安卓 : ListVIew : change background onClick

android - 如何使用 RxAndroidBLE 库使用 Android BLE 绑定(bind)功能?

html - 如何实现适用于桌面和移动设备的水平可拉伸(stretch)页面布局