M
M
mr-zherart2014-03-22 02:17:11
Android
mr-zherart, 2014-03-22 02:17:11

How to make drawable centered in button android?

There is a button that takes up half the screen height, you need to insert an icon into it so that it is centered along with the text in the middle of the screen.

<Button
      android:id="@+id/readBtn"
      android:layout_width="0dip"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/read_btn_selector"
      android:drawableTop="@drawable/read"
      android:gravity="center"
      android:onClick="readBtnClick"
      android:text="@string/read_btn"
      android:textColor="#ffffff" />

Now
899355a0e82e.jpg
Thank
87b7862309e1.jpg
you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ProkletyiPirat, 2014-03-23
@ProkletyiPirat

I recently searched for the answer to this question, but did not find it.
Did so

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/myButton"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selector_bottom_buttons"
    >

    <ImageView
        android:id="@+id/myButton_Image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:contentDescription="@string/text_btnKill"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

    <TextView
        android:id="@+id/myButton_Text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Kill"
        />
</FrameLayout>

if the size of the button is strictly fixed, then you can make a drawable from the layer-list and indent the image inside, but in my opinion FrameLayout is an easier way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question