【问题标题】:Center Aligned Button's Text on Left居中对齐左侧的按钮文本
【发布时间】:2012-11-19 13:42:04
【问题描述】:

即使我使重心文本仍然在左侧。我认为必须有一种方法来更改文本对齐 (我也试过 android:textAlignment="center")

我在下面包含了屏幕截图,但没有足够的声誉来展示它。

<!-- language: lang-xml-->    
<Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:gravity="center"
                android:text="Button" />

也试过让它 match_parent 还是一样的问题。

编辑整个 XML

<!-- language: lang-xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etCommands"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Type a Command"
        android:password="true" >
    </EditText>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Button" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="invalid" />

</LinearLayout>

ScreenShot

【问题讨论】:

  • 是的,我知道这意味着我尝试过。
  • 删除android:gravity="center"
  • 我确实添加了它以防止任何 cmets “只需添加 android:gravitity:center” 没有这个我有同样的问题
  • 你能把总的 xml 贴出来

标签: android button text-align


【解决方案1】:

我也有这个问题。只需重新启动您的 IDE。

【讨论】:

  • 重启 IDE 对我有用。希望 Eclipse 中有一个刷新或重新渲染按钮
【解决方案2】:

将 android:layout_gravity="center" 添加到按钮的布局中并设置宽度 wrap_content 应该可以修复它。

【讨论】:

  • 是的,很抱歉,我刚刚意识到您在谈论文本而不是实际按钮。我应该更仔细地阅读。会看起来多一点
  • 您想让文本“按钮”居中吗?因为在您的屏幕截图中以及当我复制您的代码并运行它时,它看起来对我来说是这样的
  • 奇怪...我刚刚将我的代码复制到我的另一台计算机上运行良好... ide 一定有问题。
  • 是的,我明白了。当我这样做时,它完全居中,所以我不知道为什么它不适合你。您可以添加 android:paddingRight="xdp" 其中 x= 可能是 20 或 30 dp