Android buttons with shape background or image

2019-09-07 09:46发布

问题:

I want to create a button in an Android application which will be round with a color of my choosing and a plus sign as its text.

Is it better (in terms of space, efficiency etc.) to create an image of the above description and set it as a background image, or it it better to make a shape with a color and add that as a background?

回答1:

To do that you can create a circle shape with the help of shape drawable, but the better way to do this is to use the FloatingActionButton which is circular in shape and you can provide the icon of your choice.



回答2:

    -First of You create drawable xml
    -ic_round_shape_background
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
      <solid android:color="#000" /><!--color code which you are use instead #000-->
      <corners android:bottomRightRadius="8dp"
          android:bottomLeftRadius="8dp"
          android:topRightRadius="8dp"
          android:topLeftRadius="8dp"/>
    </shape>

  //Create xml and use background like below
    activity_main.xml

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


回答3:

please try the below code.

create a background.xml in drawable folder and paste this code.

`

<solid android:color="@color/colorPrimary"></solid>

<stroke
    android:width="2dp"
    android:color="@color/colorPrimary"></stroke>

<padding
    android:left="5dp"
    android:right="5dp"
    android:top="5dp"></padding>

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

`

in Layout

<LinearLayout android:id="@+id/place_holder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background" android:gravity="center" android:orientation="horizontal">

                   ` <ImageView
                        android:layout_gravity="center_vertical"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/image" />


                </LinearLayout>`


标签: android shape