【问题标题】:Change shape of Button while mantaining its material design ripple effect改变 Button 的形状,同时保持其材料设计的涟漪效果
【发布时间】:2016-09-14 07:12:29
【问题描述】:

我想创建具有自定义形状的按钮,它会像主题 "Base.Widget.AppCompat.Button.Colored".

但是因为我必须自定义它的形状(圆角我必须覆盖它的android:background - 这是迄今为止我知道的唯一方法(不......不,我不会对 FrameLayout 使用肮脏的黑客)。 目前,只能通过在 xml 文件中提供我们自定义的 <shape> 作为背景可绘制对象来完成。

启用 selectableItemBackground 的最有希望的代码是:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/colorAccent"/>
            <corners android:topLeftRadius="@dimen/button_shape_radius"
                android:topRightRadius="@dimen/button_shape_radius"/>
        </shape>
    </item>

    <item android:drawable="?attr/selectableItemBackground">
        <shape>
            <solid/>
            <corners android:topLeftRadius="@dimen/button_shape_radius"
                android:topRightRadius="@dimen/button_shape_radius"/>
        </shape>
    </item>
</layer-list>

不幸的是,我无法用&lt;item android:drawable="?attr/selectableItemBackground"&gt; 塑造第二个项目,因此最终按下项目的形状是矩形。

如果有人会给我这个问题的解决方案,我会很感激。 我使用 API_MIN = 16,所以不能使用涟漪效应。我也不想使用 FrameLayout 或其他外部库,迫使我用一些东西包装 Button。

【问题讨论】:

    标签: android shape android-selector


    【解决方案1】:

    要获得低于 21 的波纹,您可以使用此库 Ripple Effect,您可以使用不带波纹标签的形状,并使用 RippleView 包围视图以获取更多详细信息,您可以查看 Usage 部分

    21+ 你可以试试这个我没有测试过,但是应该可以,把它放在文件夹 drawable-v21 中

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item>
        <shape>
            <solid android:color="@color/colorAccent"/>
            <corners android:topLeftRadius="@dimen/button_shape_radius"
                android:topRightRadius="@dimen/button_shape_radius"/>
        </shape>
    </item>
    
    <item android:drawable="?attr/selectableItemBackground">
        <shape>
            <solid/>
            <corners android:topLeftRadius="@dimen/button_shape_radius"
                android:topRightRadius="@dimen/button_shape_radius"/>
        </shape>
    </item>
    </ripple>
    

    更新

    我把Ripple Effect的源代码改成了一个按钮

    您可以获取代码here

    用法

    就像任何普通按钮一样

    XML

    <path.to.the.RippleButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_rounded_top_blue"
        android:id="@+id/mybutton"/>
    

    Java

    RippleButton button = (RippleButton) findViewById(R.id.mybutton);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainScreen.this, "You did it", Toast.LENGTH_SHORT).show();
                }
            });
    

    如果您需要其他视图,您只需对源代码进行一些调整,这很容易(在我看来)

    【讨论】:

    • 图书馆工作,但它不是最好的灵魂 4me,因为我必须在我的视图之外使用每个时间包装器,而我想声明 one 按钮样式代码复用的目的
    • 你是对的,我昨天也在尝试做同样的事情,我的应用上有 4 个视图填满了屏幕,我必须用 Rippleview 包裹每个视图,我在几个设备,我没有遇到性能问题,但我的布局很简单
    • 我没有批准你的回答,因为我希望有人想出更好的办法
    • 不用担心 :) 但现在我有点想知道如果我找到让你知道的东西是否有更好的方法
    • 我做到了 :) 并不难我们只需要更改几行,看看我的编辑你不再需要用RippleView 包装你的按钮只需将你的按钮更改为RippleButton
    猜你喜欢
    • 2015-07-11
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多