首页 > 解决方案 > 有没有可能在android中设计如下图的方法?

问题描述

我想在右侧设置背景颜色,如下图所示。我怎么能这样做?

在此处输入图像描述

标签: androidxmlandroid-layout

解决方案


创建可绘制文件 XML

half_circle.xml

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="1000dp"
        android:topLeftRadius="1000dp"
        android:bottomRightRadius="0dp"
        android:topRightRadius="0dp" />
    <gradient
        android:startColor="#32aef4"
        android:centerColor="#44a0af"

        android:endColor="#79e2e2"
        android:angle="45"/>
</shape>

在布局文件夹中创建项目布局

项目.xml

    <?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="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:text="sample"
            android:textSize="25sp"
            android:textColor="@android:color/black"/>

            <RelativeLayout
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:background="@drawable/half_circle">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_heart"
                android:layout_centerInParent="true"
                android:layout_margin="5dp"/>

            </RelativeLayout>

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

</RelativeLayout>

你可以实现以上。

PS - 请在 StackOverflow 中发布问题之前进行研究。请仔细阅读指南


推荐阅读