buttonの形や色を変える方法

①res/drawable/以下にxmlファイルを作成する。

 

例)

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

</shape>

 

例)の中の・・・の部分に変更したいbuttonの形や色やサイズなどの設定をしていく。

 

以下、設定のコード。

角が丸くなる。

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

枠線が付く。 

<stroke

         android:color="#ffffff"  

         android:width="2dp"/>

パディングが設定できる。

<padding

         android:left="10dp"

         android:top="10dp"

         android:right="10dp" 

        android:bottom="10dp" />

サイズが設定できる。

<size

         android:height="30dp" 

        android:width="30dp" />

 

 など。

 

②activity_main.xmlにレイアウトを変えたいbuttonのbackground属性を①で作成したxmlを指定する。

 

例2)

<Button

… 

   android:background="@drawable/①で作成したxmlファイル名(xmlは除く) "/>

 

③activity_main.xmlの<button>…に

「app:backgroundTint="@null"」
を入れる。