Android:按钮内部可绘制

时间:2018-01-26 00:14:48

标签: android xml drawable

我有这个按钮,我想在里面添加一个drawable:

没有绘图的按钮:

Button without drawable

这就是我想要做的事情:

带抽屉的按钮:
Button with drawable

我知道android:drawableStart但这是我在使用时得到的:

Drawable using android:drawableStart

如果有一种方法可以在按钮的边缘写入文本,那么它会很有用,因为我想要放置的可绘制字符是一个unicode字符(表情符号)。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以自己创建按钮:

<强> my_button.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:clickable="true"
              android:background="@drawable/selector_button">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:padding="5dp"
               android:id="@+id/imageView1"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView"/>

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:padding="5dp"
               android:id="@+id/imageView2"/>
</LinearLayout>

<强> selector_button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/holo_green_dark"/>
        <stroke android:width="3dp" android:color="@android:color/white"/>
    </shape>
</item>
<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/holo_green_light"/>
        <stroke android:width="2dp" android:color="@android:color/white"/>
    </shape>
</item>

<强> MyButton.java:

public class MyButton extends FrameLayout {

public MyButton(Context context) {
    this(context, null, 0);
}

public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    inflate(context, R.layout.my_button, this);
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyButton, 0, 0);
    final Drawable image1 = typedArray.getDrawable(R.styleable.MyButton_image1),
            image2 = typedArray.getDrawable(R.styleable.MyButton_image2);
    final String text = typedArray.getString(R.styleable.MyButton_txt);
    typedArray.recycle();

    if (image1 != null) ((ImageView) findViewById(R.id.imageView1)).setImageDrawable(image1);
    if (image2 != null) ((ImageView) findViewById(R.id.imageView2)).setImageDrawable(image2);
    if (text != null) ((TextView) findViewById(R.id.textView)).setText(text);
}

public void setText(int resId) {
    ((TextView) findViewById(R.id.textView)).setText(resId);
}}

创建文件&#34; values / attrs.xml&#34;:

<declare-styleable name="MyButton">
    <attr name="image1" format="reference"/>
    <attr name="txt" format="string"/>
    <attr name="image2" format="reference"/>
</declare-styleable>

并在您的布局中放置代码:

<MyButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:image1="@drawable/image1"
     app:image2="@drawable/image2"
     app:txt="@string/anyText"/>