Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有不同角半径的CardView_Android_Android Layout_Material Design_Android Cardview_Material Components Android - Fatal编程技术网

Android 具有不同角半径的CardView

Android 具有不同角半径的CardView,android,android-layout,material-design,android-cardview,material-components-android,Android,Android Layout,Material Design,Android Cardview,Material Components Android,我有下面的CardView,我想为卡中的每个角设置不同的半径。是否可以通过XML或编程更改它们?提前谢谢 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent

我有下面的CardView,我想为卡中的每个角设置不同的半径。是否可以通过XML或编程更改它们?提前谢谢

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    app:cardCornerRadius="0dp"
    app:cardElevation="0dp">
</android.support.v7.widget.CardView>

但使用默认的CardView项目。如果无法单独更改,则此库是一种很好的方法。

您可以创建自定义xml并将其命名为圆角。xml
如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="30dp"
    android:bottomLeftRadius="40dp"
    android:bottomRightRadius="50dp"/>
<solid android:color="your_background_color" />
</shape>

编辑:我刚刚注意到,这可能适用于除
CardView
之外的所有其他视图,因此请参阅以了解如何解决此问题

您好,您可以使用以下代码通过编程或xml添加它

app:cardCornerRadius="0dp"// xml
cardView.setRadius(0);
这是一个额外的谁是在寻找海拔高度

app:cardElevation="0.7dp"//xml
app:cardMaxElevation="1dp"//xml
cardView.setCardElevation(2.1f);//code
cardView.setMaxCardElevation(3f);//code
CardView的XML的完整Java表示

CardView cardView = (CardView) findViewById(R.id.cardView);
cardView.setUseCompatPadding(true);
cardView.setContentPadding(30, 30, 30, 0);
cardView.setPreventCornerOverlap(true);
cardView.setCardBackgroundColor(Color.WHITE);
cardView.setCardElevation(2.1f);
cardView.setRadius(0);
cardView.setMaxCardElevation(3f);
它需要的是官方版本(扩展了)和至少为的1.1.0

材质卡片视图添加到布局中

android:background="@drawable/rounded_corners"
    <com.google.android.material.card.MaterialCardView
        style="@style/CustomCardViewStyle"
        ...>
      
    </com.google.android.material.card.MaterialCardView>

您也可以通过编程实现它。
只需在卡的角上应用自定义设置。
比如:

float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
  cardView.getShapeAppearanceModel()
      .toBuilder()
      .setTopLeftCorner(CornerFamily.ROUNDED,..)
      .setTopRightCorner(CornerFamily.ROUNDED,..)
      .setBottomRightCorner(CornerFamily.ROUNDED,radius)
      .setBottomLeftCornerSize(0)
      .build());
    Card(
        shape = RoundedCornerShape(
           4.dp,
           8.dp,
           16.dp,
           2.dp)
    ){
        Text("Content Card")
    }
注意:它需要库的1.1.0版本


使用Jetpack compose
1.0.0
(使用
1.0.0-beta07
进行测试),您可以使用以下内容:

float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
  cardView.getShapeAppearanceModel()
      .toBuilder()
      .setTopLeftCorner(CornerFamily.ROUNDED,..)
      .setTopRightCorner(CornerFamily.ROUNDED,..)
      .setBottomRightCorner(CornerFamily.ROUNDED,radius)
      .setBottomLeftCornerSize(0)
      .build());
    Card(
        shape = RoundedCornerShape(
           4.dp,
           8.dp,
           16.dp,
           2.dp)
    ){
        Text("Content Card")
    }

注意:如果您只想在底部实现圆角,而在顶部实现规则角,这里是一个解决方法。如果您希望cardview的所有四个角具有不同的半径,则此操作将不起作用。您必须使用material cardview或使用某些第三方库

以下是对我有效的方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#F9F9F9">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/profile_bg"/>

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cvProfileHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="32dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:orientation="vertical"
            android:background="@drawable/profile_bg"
            android:id="@+id/llProfileHeader"
            android:gravity="center_horizontal">

            <!--Enter your code here-->

        </LinearLayout>
    
    </androidx.cardview.widget.CardView>

</RelativeLayout>


总共有两个cardview。第二个cardview是一个具有圆角的视图(通常在所有边上),并将在其下保存所有其他子视图。上面的第一个cardview也位于同一标高(标高),具有相同的背景,但只有第二个cardview高度的一半左右,没有圆角(只有通常的锐角)。通过这种方式,我能够在底部实现部分圆角,在顶部实现正常圆角。但对于所有四个面,您可能都必须使用材质cardview。

此解决方案设置cardview的所有角,我只想设置一个,例如左上角。好的,通过cardview直接了解您的观点,您无法轻松实现,因此我建议您使用一些第三方LIB。检查这个是的,你是对的,我已经检查了这个项目。如果我无法通过CardView获得任何解决方案,这是我的下一步。谢谢谢谢你的帮助!您是对的,这种方法不适用于CardView。我已经看过你建议的帖子了,但我认为这是一个肮脏的解决方案。我一直在寻找更干净的东西,比如从代码中修改这些属性。无论如何谢谢你!是的,我注意到这是一个解决办法。这似乎不是一个好方法。它不适用于cardview,因此此答案不再适用于此问题。嗨,Gabriel,谢谢你的回答。此示例可以在没有Android x的情况下完成?@Juanje否,新材质组件库需要@VarunRaj,它需要1.1.0(如回答中所述)。目前为1.1.0-alpha10@GabrieleMariotti-如果您可以添加适当的
实现。。。。。alpha…
这将节省像我这样的人几分钟:)@KrzysztofKubicki有一些解决方案。例如,您可以使用材质组件库中的
ShapableImageView
。如果我们只需要塑造一个图像,请使用绘制具有所提供形状的位图的材质。