我需要这样的设计,但我面临两个问题

我需要一个具有以下项目设计的列表视图模板

http://img.mukewang.com/64b7a6cc0001adb002400122.jpg

我尝试了一些找到的代码,但没有得到我需要的结果。

  <item

    android:bottom="-32dp"

    android:left="-3dp"

    android:right="-2dp"

    android:top="2dp">

    <shape android:shape="rectangle" >

        <corners android:radius="32dp"/>

        <stroke

            android:width="2dp"

            android:color="#fff" />


        <solid android:color="@android:color/transparent" />


        <padding

            android:bottom="10dp"

            android:left="10dp"

            android:right="10dp"

            android:top="0dp" />

    </shape>

</item>

这是我的线性布局背景。


MMMHUHU
浏览 98回答 2
2回答

慕少森

尽管我们可以使用 png 或 或 image 来获取此 . 但如果你真的想在这个可绘制对象上执行此操作,那么这里是代码。我尝试了一下,结果非常相似。我使用两层列表创建了一个可绘制对象,其中包含两个不同的矩形。<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:bottom="10dip">&nbsp; &nbsp; <shape&nbsp; &nbsp; &nbsp; &nbsp; android:shape="rectangle"&nbsp; &nbsp; &nbsp; &nbsp; android:useLevel="false">&nbsp; &nbsp; &nbsp; &nbsp; <corners android:radius="12dp" />&nbsp; &nbsp; &nbsp; &nbsp; <stroke&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:width="2dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:color="#ffffff" />&nbsp; &nbsp; &nbsp; &nbsp; <size&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:width="60dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:height="60dp" />&nbsp; &nbsp; &nbsp; &nbsp; <solid android:color="@android:color/transparent" />&nbsp; &nbsp; </shape></item><item android:top="20dip">&nbsp; &nbsp; <shape android:shape="rectangle">&nbsp; &nbsp; &nbsp; &nbsp; <solid android:color="#800080" />&nbsp; &nbsp; </shape></item>您可以在您的视图中使用它作为背景。我已经在我的项目中使用过这个,所以我添加了我的项目视图的代码。&nbsp;<?xml version="1.0" encoding="utf-8"?>&nbsp;<LinearLayout&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#800080"><android.support.constraint.ConstraintLayout&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:id="@+id/linearLayout"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_margin="@dimen/text_18"&nbsp; &nbsp; android:background="@drawable/sample">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/crime_title"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Crime Title"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/colorAccent"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="18sp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@+id/crime_solved"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" />&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/crime_date"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:paddingBottom="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Crime Date"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/colorAccent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="@id/crime_title"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="@id/crime_title"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/crime_title" />&nbsp; &nbsp; <ImageView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/crime_solved"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="16dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="@+id/crime_date"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/crime_title"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:srcCompat="@drawable/ic_solved" /></android.support.constraint.ConstraintLayout>希望这会有所帮助。

精慕HU

您可以为该形状创建一个9 补丁图像,而不是使用 xml drawable ,这可能会有所帮助。在您最喜欢的图像编辑器中创建该形状并将其复制到项目的可绘制目录中。在 Android Studio 中,右键单击要从中创建 NinePatch 图像的 PNG 图像,然后单击“创建 9-patch 文件”。输入 NinePatch 图像的文件名,然后单击“确定”。您的图像将使用 .9.png 文件扩展名创建。双击新的 NinePatch 文件以在 Android Studio 中将其打开。您的工作区现在将打开。左侧窗格是绘图区域,您可以在其中编辑可拉伸面片和内容区域的线条。右侧窗格是预览区域,您可以在其中预览拉伸时的图形。在 1 像素周界内单击以绘制定义可拉伸面片和(可选)内容区域的线条。右键单击(或在 Mac 上按住 Shift 键并单击)以擦除之前绘制的线条。完成后,单击“文件”>“保存”以保存更改。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java