【问题标题】:how to make specific button side curved如何使特定按钮侧弯曲
【发布时间】:2018-12-13 13:46:24
【问题描述】:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners
        android:bottomLeftRadius="30dp"
        android:topLeftRadius="30dp" />
</shape>

<Button
   android:id="@+id/btn_list"
   android:layout_width="@dimen/width_100dp"
   android:layout_height="wrap_content"
   android:background="@drawable/my_background"/>

尝试了上面的代码,但它对我不起作用。我需要做一个自定义类扩展按钮类吗?

【问题讨论】:

标签: android


【解决方案1】:

非常简单!,以 CardView 作为根元素。 CardView 会根据自身对子元素的边缘进行四舍五入。

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_margin="20dp"
    app:cardCornerRadius="25dp"
    app:cardElevation="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/app_name" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ecb8ff"
            android:gravity="center"
            android:text="@string/app_name" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

输出:

另外,如果您需要 TabLayout。可以看this answer

【讨论】:

  • 我认为这对于仅具有圆角来说是一种矫枉过正,与使用形状相比,它增加了层次结构并带有过度绘制。
  • @MelihAksoy 是的,它增加了视图的层次结构。但这对性能没有影响。您是否曾经担心过 List 中的数百个项目?这只是一个。我试图给出一个简单的解决方案。
  • 欣赏解决方案,添加评论以帮助任何寻求更轻量级解决方法的人。
【解决方案2】:

试试这个,它有点棘手,但它有效

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="500dp"
                android:topLeftRadius="0dp"
                android:topRightRadius="500dp" />
        </shape>
    </item>
</layer-list>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2022-07-20
    • 2017-01-14
    • 2013-01-07
    相关资源
    最近更新 更多