Help us understand the problem. What is going on with this article?

【Android】LinearLayoutで画像とテキストを縦に並べてボタンっぽくする

More than 3 years have passed since last update.

概要

LinearLayoutで画像とテキストを縦に並べてボタンっぽいものを作ります。

完成イメージ

タップ前:my_button.png 、タップ後:my_button_pressed.png

XML

ボタンのXML

my_button.xml
<LinearLayout
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/button_background"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="9dp"
    android:paddingRight="9dp"
    android:paddingTop="5dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/plus_symbol" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="追加"
        android:textColor="@android:color/white"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>

ボタンの背景のXML

button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/button_background_normal"
        android:state_pressed="false"
        android:state_selected="false" />
    <item
        android:drawable="@drawable/button_background_pressed"
        android:state_pressed="true"
        android:state_selected="false" />
    <item
        android:drawable="@drawable/button_background_normal"
        android:state_pressed="false"
        android:state_selected="true" />
    <item
        android:drawable="@drawable/button_background_pressed"
        android:state_pressed="true"
        android:state_selected="true" />
</selector>

タップ前のXML

button_background_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="FF9AA2BF" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="FF9AA2BF" />
</shape>

タップ後のXML

button_background_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="FF6E7280" />
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="FF6E7280" />
</shape>

+画像

app/src/main/res/drawable/plus_smybol.png
plus_symbol.png
(↑白い+記号の画像です)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away