【问题标题】:How to change icon size in material TabItem?如何更改材质 TabItem 中的图标大小?
【发布时间】:2021-03-04 09:06:41
【问题描述】:

我正在使用material library,并使用TabLayout 和三个TabItem。在这个TabItem 中,我使用了图标,我想让它变大,但我不知道是否可以更改图标大小。

这是我现在的 XML:

  <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline6"
        app:tabBackground="@drawable/tab_background_color_selector"
        app:tabIconTint="@color/white"
        app:tabIndicator="@null">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/icon_one"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/icon_two"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/icon_three"/>

    </com.google.android.material.tabs.TabLayout>

【问题讨论】:

  • 可以为您的 TabItems 提供完全自定义的视图(在代码中,而不是在 xml 中)。例如,请参阅this question。所以你基本上可以增加它们的大小,如果你愿意,可以做更多的事情。
  • stackoverflow.com/a/35372182/5684956 您可以使用自定义布局来灵活调整标签项的大小

标签: android android-layout material-design


【解决方案1】:

您可以创建自己的自定义选项卡布局,其中包含 imageView。

例如: 我已将其命名为 my_custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:id="@+id/icon"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

然后在你的 MainActivity 类中你可以这样做

View view = getLayoutInflater().inflate(R.layout.my_custom_tab, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setBackgroundResource(R.drawable.your_drawable);
tabLayout.addTab(tabLayout.newTab().setCustomView(view));

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 2019-04-11
    • 2018-10-31
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2021-05-23
    • 2017-05-20
    相关资源
    最近更新 更多