Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

java - How can I develop this kind of Button

This button has outer white rong and an inner grey background.
The inner background contains an image (tickmark) and a text (Apply).

http://imgur.com/a/NfXKR

I am able to make a shape like this (below).

http://imgur.com/k0cSyM3

I am using a shape in drawable (button.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval">
        <size android:height="85dp"
            android:width="90dp"/>
        <solid android:color="@color/grey"/>
        <stroke android:color="@color/white" android:width="4dp"></stroke>
    </shape>
</item>
<item
    android:drawable="@drawable/checkmark_filled"
    android:bottom="20dp"
    android:left="20dp"
    android:right="20dp"
    android:top="20dp"/>
</layer-list>

and using an ImageView to use the shape

<ImageView
    android:id="@+id/button_apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button"/>

But I am not able to make a text in shape.
Am I missing something or is there any way possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I would do is something like this:

1 - Separate your LayerList into 2 distinct drawables

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
        <size android:height="85dp" android:width="90dp" />
        <solid android:color="@color/grey" />
        <stroke android:color="@color/white" android:width="4dp" />
</shape>

I assume you already have this bitmap: drawable/checkmark_filled

2 - Use a TextView, instead of an ImageView:

<TextView
    android:id="@+id/button_apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgound="@drawable/button"
    android:drawableTop="drawable/checkmark_filled"
    android:text="APPLY"
    android:clickable="true"
/>

Adjust the gravity and the paddings as needed.
Also set some other properties as needed.

Note that you can (and should) use a string resource, instead of a hard-coded text.


Brief explanation:

  • I'm using the oval shape as the TextView brackground.
  • I'm using the checkmark bitmap as a compound drawable.
  • I'm using the TextView's text to write something.
  • I set the TextView as clickable, so you can use it as if it was a Button.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...