18

このような形のボタンを作りたい

ここに画像の説明を入力

xml でこれを行う方法はありますか? いくつかのポイントを設定するように、私の場合は 5..

4

3 に答える 3

25

必要なのはshape xml file、プロジェクトのdrawable-xxxフォルダーに を作成し、この形状をボタンの背景として使用することです。

と呼ばれる形状ファイルは次のarrow_shape.xmlとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Colored rectangle-->
<item>
    <shape android:shape="rectangle">
        <size 
            android:width="100dp"
            android:height="40dp" />
        <solid android:color="#5EB888" />
        <corners android:radius="0dp"/>
    </shape>
</item>

<!-- This rectangle for the top arrow edge -->
<!-- Its color should be the same as the layout's background -->
<item
    android:top="-40dp"
    android:bottom="65dp"
    android:right="-30dp">
    <rotate
        android:fromDegrees="45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

<!-- This rectangle for the lower arrow edge -->
<!-- Its color should be the same as the layout's background -->
<item
    android:top="65dp"
    android:bottom="-40dp"
    android:right="-30dp">
    <rotate
        android:fromDegrees="-45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

</layer-list>

次に、たとえばボタンの背景として使用します

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/arrow_shape"/>

スクリーンショットは次のとおりです。

ここに画像の説明を入力

詳細についてLayer-Listは、こちらを参照してください

編集:

ただし、シェイプの幅と高さに特定の値を使用したことに注意してください。それらを変更する場合は、 の値を変更する必要がある場合がありますtop, bottom and right attributesvaluesしたがって、その場合は、プロジェクトのディレクトリで別の値を使用することを検討してください。

于 2014-10-02T13:41:10.727 に答える