上の画像のようなボタンを作成しようとしています。最初の私のアイデアは、9パッチを作成し、ボタンの背景として設定することでした。しかし、これは単純なボタンなので、画像を使用せずに何とか描画できると思います。
ボタンの背景色は#0c0c0c、境界線の色は#1a1a1a、テキストの色は#ccccccです。
私はSO=で同様の質問を見つけましたが、それは勾配を作成します-
Android-ボタンの境界線
Android開発者ガイドには、これに関する詳細なガイドがあります Shape Drawbables 。
また、提供したリンクからgradient
要素を削除することもできます。
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:shape="rectangle">
<corners Android:radius="3dp" />
<stroke Android:width="5px" Android:color="#1a1a1a" />
</shape>
<Button
Android:id="@+id/button1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_above="@+id/textView1"
Android:layout_alignLeft="@+id/textView1"
Android:layout_marginBottom="56dp"
Android:text="Button"
Android:textColor="#FF0F13"
Android:background="@drawable/bbkg"/>//create bbkg.xml in drawable folder
bbkg.xml // button background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item Android:state_pressed="true"
Android:drawable="@drawable/pressed" />
<item Android:state_focused="false"
Android:drawable="@drawable/normal" />
</selector>
normal.xml //ボタンの背景の通常の状態
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:shape="rectangle">
<solid Android:color="#10EB0A"/>
<stroke Android:width="3dp"
Android:color="#0FECFF" />
<padding Android:left="5dp"
Android:top="5dp"
Android:right="5dp"
Android:bottom="5dp"/>
<corners Android:bottomRightRadius="7dp"
Android:bottomLeftRadius="7dp"
Android:topLeftRadius="7dp"
Android:topRightRadius="7dp"/>
</shape>
pressed.xml //ボタンの背景が押された状態
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:shape="rectangle">
<solid Android:color="#FF1A47"/>
<stroke Android:width="3dp"
Android:color="#0FECFF"/>
<corners Android:bottomRightRadius="7dp"
Android:bottomLeftRadius="7dp"
Android:topLeftRadius="7dp"
Android:topRightRadius="7dp"/>
</shape>
このようなものが欲しいなら
ボタンのプレビュー
これがコードです。
1. mybutton.xmlのようなドローアブルフォルダーにxmlファイルを作成し、次のマークアップを貼り付けます。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android" >
<item Android:state_pressed="true" >
<shape Android:shape="rectangle" >
<corners Android:radius="3dip" />
<stroke Android:width="1dip" Android:color="#5e7974" />
<gradient Android:angle="-90" Android:startColor="#345953" Android:endColor="#689a92" />
</shape>
</item>
<item Android:state_focused="true">
<shape Android:shape="rectangle" >
<corners Android:radius="3dip" />
<stroke Android:width="1dip" Android:color="#5e7974" />
<solid Android:color="#58857e"/>
</shape>
</item>
<item >
<shape Android:shape="rectangle" >
<corners Android:radius="3dip" />
<stroke Android:width="1dip" Android:color="#5e7974" />
<gradient Android:angle="-90" Android:startColor="#8dbab3" Android:endColor="#58857e" />
</shape>
</item>
</selector>
2.ビューの背景にこのドローアブルを使用します。ビューがボタンの場合、次のようになります。
<Button
Android:id="@+id/button1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:padding="10dp"
Android:textColor="#ffffff"
Android:background="@drawable/mybutton"
Android:text="Buttons" />