首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓CardView的彩色顶部部分,同时保持圆角

安卓CardView的彩色顶部部分,同时保持圆角
EN

Stack Overflow用户
提问于 2017-06-05 14:25:15
回答 3查看 2.7K关注 0票数 2

所以我觉得我错过了一些东西,但我就是想不通。所以我想在我的CardView的顶部做一个小的色带,但是如果我使用框架布局,或者任何一种布局并设置背景颜色,它会覆盖CardView的圆角。以下是该问题的一些代码和屏幕截图。提前谢谢你!

代码语言:javascript
复制
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardCornerRadius="10dp">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="16dp"
    android:layout_gravity="end"
    android:background="@color/colorPrimaryDark"
    android:clipChildren="true"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/menuCardImage"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="8dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="8dp"
        android:layout_toRightOf="@+id/menuCardImage"
        android:orientation="vertical">

        <TextView
            android:id="@+id/menuCardName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>
</android.support.v7.widget.CardView>

编辑:原来这只是android studio的显示问题。当我安装应用程序时,它工作正常。

EN

回答 3

Stack Overflow用户

发布于 2017-06-05 16:37:05

不是将颜色设置为背景,而是使用此可绘制设置背景。

drawable文件夹中创建top_strip.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>
    <solid android:color="#FFffffff"/>
</shape>

将半径设置为卡片边角半径,并根据需要更改颜色。看起来不错

票数 2
EN

Stack Overflow用户

发布于 2018-08-31 00:20:38

您现在可以设置CardView颜色。

代码语言:javascript
复制
color = ContextCompat.getColor(cardView.getContext(), R.color.white);
cardView.setCardBackgroundColor(color);

这是最简单的方法。

票数 1
EN

Stack Overflow用户

发布于 2019-05-17 10:33:06

我认为你应该尝试使用app:cardBackgroudColorCardView.setCardBackgroundColor。这对我很有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44362879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档