【问题标题】:Alignment of text on button按钮上的文本对齐
【发布时间】:2011-07-13 11:32:03
【问题描述】:

我正在创建一个自定义按钮。我想设置按钮上文字的位置。

  • 我有两个按钮的背景图片(button_off 和 button_on),第一个是普通按钮,第二个是 onFocus / onPressed 状态。

  • 现在我必须将文本放在按钮上,然后它应该如下所示:

但我无法在此背景按钮图像上设置文本的确切位置。 请分享您解决此问题的建议。

【问题讨论】:

  • 点击这个链接Buttons,你会在这里得到答案。浏览这个网站你会学到很多东西
  • 我在链接页面上找不到答案。此外,我认为发布链接/文章集作为特定问题的答案是一种不好的风格。如果可以在文章中的某处找到答案,至少直接链接到文章即可。

标签: android button alignment


【解决方案1】:

您可以尝试使用 9-patch 图像作为背景。更多信息请点击此处:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

【讨论】:

  • 但是正如您在按钮图像中看到的那样,文本不在按钮的中心。比中心多 5 个像素。那么该怎么做呢?
  • 当您使用 9-patch 作为背景时,您手动(使用 Google 的“draw9patch”工具)定义可拉伸的区域,以及您的文本将出现的区域。我认为它应该可以解决您的问题。
【解决方案2】:

您可以在 XML 文件中设置这些属性,`

<Button
    android:id="@+id/button1"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:drawableLeft="@android:drawable/ic_lock_lock"
    android:text="All" 
    android:gravity="center"/>

`

【讨论】:

    【解决方案3】:

    对齐文本,使用重力选项!或尝试使用填充

    【讨论】:

    • 我们只能将填充用于布局,不能用于文本。通过设置重力,我们只能设置左/右/上/下/中心对齐。但我想将文本设置为比中心多 5 像素..
    • 我甚至可以对文本使用填充!
    【解决方案4】:
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@android:drawable/arrow_down_float"
        android:background="@drawable/btn_all_bg"
        android:text="All">
     Button>
    

    您可以使用drawableLeft/right/top/bottom在按钮上添加图标,文本会自动调整。

    【讨论】:

    • 最好和最简单的方法。感谢您的回答
    【解决方案5】:

    几种方法:

    1. 定义一个按钮并尝试android:drawableStart
    2. 扩展 Button 并绘制自己
    3. 尝试定义一个LinearLayout,其中包含一个文本和一个图像 按钮的背景, 将它们的权重设置为 1 并将它们放入另一个 LinearLayout 并使其成为 onClick 不能保证 3 无需一两次调整就能正常工作,但值得一试

    【讨论】:

      【解决方案6】:

      您可以将布局用作按钮style="@android:style/Widget.Button",并根据需要进行配置。

      将此粘贴​​到您的 xml 文件中:

      <LinearLayout
      android:id="@+id/button"
      style="@android:style/Widget.Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" >
      <TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="2dp"
          android:text="TextView" />
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-28
        • 1970-01-01
        • 2013-08-20
        • 2017-11-04
        • 2021-12-26
        • 1970-01-01
        • 2013-03-07
        • 2020-10-05
        相关资源
        最近更新 更多