【问题标题】:Button with image and text centered [duplicate]图像和文本居中的按钮[重复]
【发布时间】:2012-07-06 22:04:56
【问题描述】:

我需要中间有文字的按钮和文字左边的图标。

我的按钮:

<Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:onClick=""
            android:text="@string/search_button"
            android:textColor="@drawable/button_text"
            android:layout_weight="1"
            android:background="@drawable/button"
            android:drawableLeft="@drawable/new_star_nonactive" />

我需要宽度为 fill_parent 的按钮(所有屏幕宽度)

但我有这个:

如何在文本附近设置图像位置中心?

【问题讨论】:

    标签: android image button


    【解决方案1】:

    试试这个你一定会得到结果:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         >
    
        <Button 
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hi"
            android:textSize="24sp"
            android:background="@drawable/wood"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btn"
            android:layout_alignBottom="@+id/btn"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="90dp"
            android:background="@drawable/ic_launcher"
            android:gravity="center" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个,

      <Button 
          android:background="@null"
          android:layout_marginLeft="0dp" 
          android:text="HOME"
          android:layout_width="62dp"
          android:marqueeRepeatLimit="marquee_forever"
          android:textColor="#ffffff" 
          android:layout_height="50dp" 
          android:ellipsize="marquee" 
          android:drawableLeft="@drawable/icon_home" 
          android:id="@+id/homebtn" 
          android:singleLine="true"
          android:textSize="10dp"
         android:paddingTop="6dp"
          android:layout_alignParentLeft="true">
        </Button>
      

      【讨论】:

        【解决方案3】:

        那么唯一的方法就是使用填充。在您的情况下,您希望图像向右移动,因此请使用 android:paddingLeft="50dp" 或您想要移动的下降量。

        编辑: 还要添加android:gravity="left | center" 以使文本与图像正确。

        【讨论】:

        • 但我不知道什么填充......在不同的屏幕上会有所不同
        • 好吧,这是唯一的方法。另外,我认为在 xml 而不是 Java 代码中指定下降时,android 会尊重这一点,但不是 100% 肯定。
        • 或者如果你想确定,你可以为不同的屏幕创建不同的布局,并将它们放在 layout-hdpi、layout-mdpi 等中。
        • 填充将适用于文本和图像,这也会移动文本,这不是必需的。
        • @AkashG 你是对的,所以我现在进行了编辑,这将解决文本的这个问题。
        猜你喜欢
        • 2018-03-04
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-05
        • 2018-02-08
        • 2014-08-08
        • 1970-01-01
        相关资源
        最近更新 更多