开发者

How to center drawableTop and text vertically on the Button in Android?

I have the Button defined as f开发者_Go百科ollows:

<Button
 android:drawableTop="@drawable/ico"
 android:gravity="center"
 android:id="@+id/StartButton2x2"
 android:text="@string/Widget2x2StartBtnLabel"
 android:layout_width="0dip" 
 android:layout_height="fill_parent"
 android:layout_weight="1" 
 android:background="@drawable/widgetbtn_bg"
/>

The problem is that 'drawableTop' image is aligned to the top border of the Button view. I would like to center it (together with text label) vertically on the button.

'android:gravity' seems to work only on text label. It does not affect 'drawableTop' positioning.

I was able to center it vertically using 'android:paddingTop' - but that does not seem to be a good idea. I guess it would not work properly on different screen resolution/size.


New answer

Apparently I misunderstood the question. The answer is to use:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    style="@android:style/Widget.Button">

...and enclose an ImageView and a TextView inside.


Old answer

Here's a thought: wrap your button in a FrameLayout. Soemthing like:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/ico"
    android:foregroundGravity="center">
    <Button
        android:gravity="center_vertical"
        android:id="@+id/StartButton2x2"
        android:text="@string/Widget2x2StartBtnLabel"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:background="@drawable/widgetbtn_bg" />
</FrameLayout>

Btw, what's up with android:layout_width="0dip"? Doesn't seem right.


'android:gravity' seems to work only on text label. It does not affect 'drawableTop' positioning.

Correct.

I was able to center it vertically using 'android:paddingTop' - but that does not seem to be a good idea. I guess it would not work properly on different screen resolution/size.

You might be able to solve this by using dimension resources.

However, other than via padding, you cannot "center drawableTop and text vertically on the Button". The drawable feature of Button is not very configurable.


You can use textview with its property.

<LinearLayout
            style="@android:style/Widget.Button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/.."
                android:text="" />
        </LinearLayout>


<Button
  android:id="@+id/....."
  android:...............
  .......................
  android:gravity="center_horizontal"
/>

I was ok with it.


Use android:drawableTop="@drawable/Icon" instead of background

Don't forget to change

android:layout_width="fill_parent"
android:layout_height="wrap_content"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜