如何在Android中像Iphone一样使用Tab Host?

问题描述 投票:-1回答:1

我是Android新手。我正在做android应用程序。我必须像Android一样在Android中做TabHost,我正在共享图像。请帮助我进行布局。

我正在尽力而为,但没有确切的解决方案。

enter image description here

android android-layout styles android-tabhost android-style-tabhost
1个回答
0
投票

您可以应用自定义形状和选项卡选择器。

shape_tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- radius should be half of the desired TabLayout height -->
    <corners android:radius="15dp" />
    <solid android:color="@color/colorWhite"/>

</shape>

shape_tab_un_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- color of the selected tab -->
    <solid android:color="@android:color/transparent" />


</shape>

selector_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- drawable for selected tab -->
    <item
        android:drawable="@drawable/shape_tab_selected"
        android:state_selected="true"/>

    <!-- drawable for unselected tab -->
    <item
        android:drawable="@drawable/shape_tab_unselected"
        android:state_selected="false"/>

</selector>

在活动布局中添加选项卡布局并将selector_tab设置为tabBackground

<com.google.android.material.tabs.TabLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGray"
app:tabGravity="fill"
app:tabBackground="@drawable/selector_tab"
app:tabIndicatorColor="@android:color/transparent"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabRippleColor="@null"
app:tabSelectedTextColor="@color/colorBlack"
app:tabTextColor="@color/colorBlack"  />

根据您的需要自定义其他属性。您可以找到有关TabLayout here

的更多信息

就这些。

快乐编码! :)

© www.soinside.com 2019 - 2024. All rights reserved.