2 回答

TA貢獻2019條經驗 獲得超9個贊
盡管我們可以使用 png 或 或 image 來獲取此 . 但如果你真的想在這個可繪制對象上執行此操作,那么這里是代碼。
我嘗試了一下,結果非常相似。
我使用兩層列表創建了一個可繪制對象,其中包含兩個不同的矩形。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="10dip">
<shape
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="12dp" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<size
android:width="60dp"
android:height="60dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:top="20dip">
<shape android:shape="rectangle">
<solid android:color="#800080" />
</shape>
</item>
您可以在您的視圖中使用它作為背景。我已經在我的項目中使用過這個,所以我添加了我的項目視圖的代碼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_18"
android:background="@drawable/sample">
<TextView
android:id="@+id/crime_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Crime Title"
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="@+id/crime_solved"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/crime_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="8dp"
android:text="Crime Date"
android:textColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="@id/crime_title"
app:layout_constraintStart_toStartOf="@id/crime_title"
app:layout_constraintTop_toBottomOf="@+id/crime_title" />
<ImageView
android:id="@+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="@+id/crime_date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/crime_title"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_solved" />
</android.support.constraint.ConstraintLayout>
希望這會有所幫助。

TA貢獻1845條經驗 獲得超8個贊
您可以為該形狀創建一個9 補丁圖像,而不是使用 xml drawable ,這可能會有所幫助。
在您最喜歡的圖像編輯器中創建該形狀并將其復制到項目的可繪制目錄中。
在 Android Studio 中,右鍵單擊要從中創建 NinePatch 圖像的 PNG 圖像,然后單擊“創建 9-patch 文件”。
輸入 NinePatch 圖像的文件名,然后單擊“確定”。您的圖像將使用 .9.png 文件擴展名創建。
雙擊新的 NinePatch 文件以在 Android Studio 中將其打開。您的工作區現在將打開。左側窗格是繪圖區域,您可以在其中編輯可拉伸面片和內容區域的線條。右側窗格是預覽區域,您可以在其中預覽拉伸時的圖形。
在 1 像素周界內單擊以繪制定義可拉伸面片和(可選)內容區域的線條。右鍵單擊(或在 Mac 上按住 Shift 鍵并單擊)以擦除之前繪制的線條。
完成后,單擊“文件”>“保存”以保存更改。
添加回答
舉報