【问题标题】:How to get square effect rather than ripple on android BottomNavigationView?如何在 android BottomNavigationView 上获得方形效果而不是波纹?
【发布时间】:2017-12-14 10:29:01
【问题描述】:

我得到了什么:

我的目标是:

我通过将它添加到我的 build.gralde 中获得了第一个:

compile 'com.android.support:design:25.3.1'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.2.4'

然后这是我的布局 xml:

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
            android:id="@+id/bottomView"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            app:menu="@menu/bottom_nav_items"
            android:background="#f8f8f8"/>

【问题讨论】:

    标签: android xml layout navigation bottomnavigationview


    【解决方案1】:

    为 BottomNavigationView 使用自定义布局。并且您的布局的可点击父级应包含属性 android:background="?selectableItemBackground" 。 下面是一个例子。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackground"
    android:clickable="true"
    android:duplicateParentState="true"
    android:gravity="center"
    android:orientation="vertical">
    
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
    
    
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sel" />
    

    这将提供 kitkat 上方的矩形 Ripple 和 kitkat 下方和上方的正常矩形的体验。如果您根本不想要波纹(在所有 android 版本中)。使用选择器作为背景。

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#4C000000" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/transparent" />
        </shape>
    </item>
    

    改用这个。

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="?selectableItemBackground"
        />
    

    【讨论】:

    • 是的,我的目标是矩形波纹效果
    • 立即使用。它应该工作。我现在无法测试自己,但试一试,让我知道。
    • 我有这个: 我已将 android:background="?selectableItemBackground" 添加到 LinearLayout 中,但仍然得到相同的圆形波纹影响(没有矩形)
    • 当我将 app:itemBackground="?selectableItemBackground" 添加到 BottomNavigationView 时它起作用了。非常感谢!
    • 查看更新答案。哈哈哈这就是我刚刚更新的内容。抱歉,我花了一些时间来识别。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2015-09-01
    • 2018-12-01
    • 2019-07-22
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多