1. ホーム
  2. Android開発

アンドロイドスタジオに角丸ボタンが実装される

2022-02-28 05:52:04
<パス

操作手順

1. drawable に button_circle_shape.xml を新規に作成します。

しかし、drawableから右クリックしてもxmlが作成されないため、以下のようにこのxmlを作成する操作が必要です。

右クリック res->新規作成->Android resourse file

Root 要素は選択可能ではなく、shape として直接記入されていることに注意してください。デフォルトは selector であるべきで、shape に変更するだけです。

そして、xml が正常に drawable に追加されたことを確認します。

2. コードを記入する

button_circle_shape.xml を読み取ります。
このxmlは、丸みを帯びたボタンのスタイルを指定するシェイプを持つ

<?xml version="1.0" encoding="UTF-8"? >
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <! -- The color of the fill -->
    <solid android:color="#FFFFFF" />
    <! -- android:radius The radius of the arc -->
    <! -- set the four corners of the button to be curved -->
    <corners 
    android:radius="5dip" />  
    <! -- can also be set separately -->
    <! -- <corners -->
   <! -- android:topLeftRadius="10dp"-->
   <! -- android:topRightRadius="10dp"-->
   <! -- android:bottomRightRadius="10dp"-->
  <! -- android:bottomLeftRadius="10dp"-->
 <! -- /> -->
        **Setting text padding**
    <! -- padding: the spacing between the text inside the Button and the Button border -->
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"
        />
</shape>



3. 形状を利用する

ボタンのbackgroundプロパティに、作成したばかりの

android:background="@drawable/button_circle_shape"