簡體   English   中英

Android:按鈕內部可繪制

[英]Android: Drawable inside of button

我有一個我想在其中添加可繪制對象的按鈕:

沒有可繪制的按鈕:

沒有可繪制的按鈕

這是我想做的:

具有可繪制的按鈕:
帶有可繪制的按鈕

我知道android:drawableStart但這是我在使用它時得到的:

使用android:drawableStart繪制

如果有一種方法可以在按鈕的邊緣上寫文本,這將很有用,因為我要放入的drawable有一個unicode字符(表情符號)。

謝謝!

您可以創建自己的按鈕:

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);
}}

創建文件“ values / attrs.xml”:

<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"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM