【问题标题】:How to create a tablayout similar view?如何创建一个类似tablayout的视图?
【发布时间】:2019-05-02 14:11:04
【问题描述】:

我正在尝试创建一个类似于TabLayout 的简单布局,但我不需要实际的选项卡。它就像一个普通的按钮。
我不确定这是否应该通过背景可绘制(以处理状态和线条颜色的变化)或仅通过视图来完成。
对此有何建议?

【问题讨论】:

    标签: android android-layout android-linearlayout android-view android-tablayout


    【解决方案1】:

    您可以将单独的按钮/相对/图像布局作为按钮。并在 frameLayout 中更改片段。我不能说这是好的做法,但同时我不知道你的情况。但我确信它会起作用,您可以轻松自定义流程和视图的每个部分。

    【讨论】:

    • change fragments in frameLayout 是什么意思?
    • @Jim TabLayout 已准备好使用提供给开发人员的结构。没有人禁止您使用自定义视图以自己的方式做同样的事情。因此,如果功能不够,您可以自己制作。第一个变体借助单独的部分/第二个覆盖和扩展来实现。
    • 不是功能不够。我认为该组件旨在用于更改屏幕中的“选项卡”。我只想拥有一个看起来像选项卡布局的组件,即底部有行分隔符。我可以为此目的扩展TabLayout 还是滥用?
    【解决方案2】:

    使用自定义的TabLayout

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="40dp"        // select your height
            app:tabBackground="@drawable/buttons_selector"  // this to customize tabs
            app:tabSelectedTextColor="@color/your_tab_text_color"
            app:tabTextAppearance="@style/TabTextAppearance"
            app:tabIndicatorHeight="0dp"    // hide the indicator line
            app:tabGravity="fill"           // or center
            app:tabMode="fixed"            // or scrollable
        >
    

    可绘制的buttons_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- drawable for selected tab -->
        <item
            android:drawable="@drawable/button_selected"
            android:state_selected="true"/>
    
        <!-- drawable for unselected tab -->
        <item
            android:drawable="@drawable/button_not_selected"
            android:state_selected="false"/>
    </selector>
    

    可绘制的 button_not_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <!-- unselected tab background -->
        <solid
            android:color="@android:color/transparent" />
    

    drawable button_selected

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <corners  android:topLeftRadius="10dp" android:topRightRadius="10dp"
            android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" />
    
        <!-- color of the selected tab -->
        <solid
            android:color="@color/your_color"/>
    </shape>
    

    您可以使用颜色、半径等。

    【讨论】:

    • 这应该有`android.support.design.widget.TabItem`作为孩子吗?
    • 是的。因此,与其创建一个用作TabLayout 的新组件,不如使用一个TabLayout(及其所有功能)和用作TabItemsTabItems
    猜你喜欢
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多