gpt4 book ai didi

android - Google Apps 中基于 Material Design 2 的自定义选择标签指示器

转载 作者:行者123 更新时间:2023-12-04 17:34:25 25 4
gpt4 key购买 nike

我正在寻找一种方法来制作基于 Google 最新 Material Design 的圆丸/圆角选项卡指示器。我制作了一个具有所需结果的可绘制对象并将其实现为“app:tabIndicator”,但它无法正常工作。

This is what I want to achieve

rounded_tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:bottom="5dp"
android:end="5dp"
android:start="5dp">
<shape
android:shape="rectangle"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="5dp">
<corners android:radius="8dp" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.tourguideapp.MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/htab_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">

<ImageView
android:id="@+id/tour_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dscn0585"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.80" />

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.3"
android:background="@android:color/black"
android:fitsSystemWindows="true" />

<androidx.appcompat.widget.Toolbar
android:id="@+id/htab_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:layout_marginBottom="40dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
style="@style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabIndicatorHeight="5dp"
app:tabIndicator="@drawable/rounded_tab_indicator"
app:layout_collapseMode="pin"
app:tabMode="scrollable" />

</com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

标签指示器可绘制:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="100dp"
android:height="2dp" />
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp">
</corners>
<solid
android:color="#1475e7">
</solid>
</shape>

标签布局:

<com.google.android.material.tabs.TabLayout
...
app:tabIndicatorFullWidth="false"
app:tabIndicator="@drawable/tab_indicator"
... />

关于android - Google Apps 中基于 Material Design 2 的自定义选择标签指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227175/

25 4 0