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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2644570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:45:10  来源:igfitidea点击:

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

xmlandroidlayoutbutton

提问by grzaks

I have the Button defined as follows:

我的按钮定义如下:

<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.

问题是“drawableTop”图像与按钮视图的顶部边框对齐。我想在按钮上垂直居中(与文本标签一起)。

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

'android:gravity' 似乎只适用于文本标签。它不会影响“drawableTop”定位。

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.

我能够使用 'android:paddingTop' 将它垂直居中 - 但这似乎不是一个好主意。我想它在不同的屏幕分辨率/大小上无法正常工作。

采纳答案by Felix

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 ImageViewand a TextViewinside.

...并附上一个ImageView和一个TextView里面。



Old answer

旧答案

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

这是一个想法:将您的按钮包装在 FrameLayout 中。类似的东西:

<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.

Btw,怎么了android:layout_width="0dip"?好像不太对

回答by CommonsWare

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

'android:gravity' 似乎只适用于文本标签。它不会影响“drawableTop”定位。

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.

我能够使用 'android:paddingTop' 将它垂直居中 - 但这似乎不是一个好主意。我想它在不同的屏幕分辨率/大小上无法正常工作。

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 Buttonis not very configurable.

但是,除了通过填充之外,您不能“在按钮上垂直居中 drawableTop 和文本”。的可绘制功能Button不是很可配置。

回答by Jithu

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>

回答by Zin Win Htet

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

I was ok with it.

我没问题。

回答by Clau

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"