【问题标题】:Create rounded corners on View在视图上创建圆角
【发布时间】:2019-11-14 21:11:12
【问题描述】:

有没有办法在LinearLayout 内创建View 的圆角而不是使用shape

<View
 android:layout_width="48dp"
 android:clickable="true"
 android:layout_height="48dp"
 android:background="#f55151" />

【问题讨论】:

  • 你试过用CardView??
  • @NileshRathod 否
  • 您可以使用CardView 或尝试使用app:cardCornerRadius="20dp"
  • 检查this

标签: android view rounded-corners cornerradius


【解决方案1】:

如果你不想使用形状 CardView 是最简单的方法: 有关更多详细信息,请查看official docs

首先将这一行添加到您的依赖项中:

implementation 'com.android.support:cardview-v7:28.0.0'

然后像这样使用它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    ... >
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>
</LinearLayout>

您可以找到更多选项here,例如:圆角半径、高度、背景颜色等。

【讨论】:

    【解决方案2】:

    您可以使用 CardView 并在 cardview 中创建自己的视图。在 CardView 上可以设置 card_view:cardCornerRadius。

    <androidx.cardview.widget.CardView
       xmlns:card_view="http://schemas.android.com/apk/res-auto"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       card_view:cardBackgroundColor="@color/yourColor"
       card_view:cardCornerRadius="6dp">
    
       <View
          android:layout_width="48dp"
          android:clickable="true"
          android:layout_height="48dp"
          android:background="#f55151" />
    
    </androidx.cardview.widget.CardView>
    

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      相关资源
      最近更新 更多