加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:55:38 所属栏目:百科 来源:网络整理
导读:我的按钮定义如下: 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:l
我的按钮定义如下:
<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"
/>

问题是“drawableTop”图像与Button视图的顶部边框对齐.我想把它(连同文字标签)垂直放在按钮上.

“android:gravity”似乎只能在文本标签上工作.它不影响’drawableTop’定位.

我可以使用“android:paddingTop”垂直居中,但这似乎并不是个好主意.我想这不会在不同的屏幕分辨率/大小上正常工作.

新答案

显然我误解了这个问题.答案是使用:

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

…并在其中包含一个ImageView和一个TextView.

老回答

这是一个想法:将你的按钮包装在一个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,android是什么?layout_width =“0dip”?看起来不对

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读