简体   繁体   English

如何在Android中的Button上垂直居中drawableTop和文本?

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

I have the Button defined as follows: 我将Button定义如下:

<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. 问题是'drawableTop'图像与Button视图的顶部边框对齐。 I would like to center it (together with text label) vertically on the button. 我想将它(与文本标签一起)垂直放在按钮上。

'android:gravity' seems to work only on text label. 'android:gravity'似乎只适用于文本标签。 It does not affect 'drawableTop' positioning. 它不会影响'drawableTop'定位。

I was able to center it vertically using 'android:paddingTop' - but that does not seem to be a good idea. 我能够使用'android:paddingTop'垂直居中 - 但这似乎不是一个好主意。 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. ...并在其中包含ImageViewTextView


Old answer 老答案

Here's a thought: wrap your button in a FrameLayout. 这是一个想法:将您的按钮包装在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" ? android:layout_width="0dip"一下, android:layout_width="0dip"什么用android:layout_width="0dip" Doesn't seem right. 似乎不对。

'android:gravity' seems to work only on text label. 'android:gravity'似乎只适用于文本标签。 It does not affect 'drawableTop' positioning. 它不会影响'drawableTop'定位。

Correct. 正确。

I was able to center it vertically using 'android:paddingTop' - but that does not seem to be a good idea. 我能够使用'android:paddingTop'垂直居中 - 但这似乎不是一个好主意。 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". 但是,除了通过填充之外,您不能“在Button上垂直居中drawableTop和文本”。 The drawable feature of Button is not very configurable. Button的可绘制功能不是很容易配置。

You can use textview with its property. 您可以将textview与其属性一起使用。

<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 使用android:drawableTop="@drawable/Icon"而不是背景

Don't forget to change 不要忘记改变

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM