【问题标题】:Android button text alignment not workingAndroid按钮文本对齐不起作用
【发布时间】:2019-11-07 16:59:52
【问题描述】:

所以我正在开发一个 android 应用程序,我以前做过,以前从未遇到过这个问题。

<Button
    android:id="@+id/LoginFormLoginBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="18dp"
    android:layout_marginEnd="40dp"
    android:layout_marginRight="40dp"
    android:text="@string/login"
    android:background="@drawable/rounded_buttons"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LoginFormPasskey" />

所以你可以很直接地看到,只有文本应该居中,这是以前的,但突然没有任何代码改变它现在稍微向右对齐。我不知道为什么。

我在stackoverflow上四处搜索,没有发现任何人提出的任何此类错误。

ScreenShot这是它的样子

【问题讨论】:

  • 您是否尝试将gravity="centre" 赋予按钮?您所经历的事情不应该正常发生。
  • 我确实尝试过,情况没有改变,我发现我设置的边距被应用于背景资源,但文本仅居中对齐,对于 android:layout_width= “match_parent”

标签: android button alignment


【解决方案1】:

将布局宽度改为 0dp

<Button
    android:id="@+id/LoginFormLoginBtn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="18dp"
    android:layout_marginEnd="40dp"
    android:layout_marginRight="40dp"
    android:text="@string/login"
    android:background="@drawable/rounded_buttons"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/LoginFormPasskey" />

【讨论】:

  • 哇,成功了,你能解释一下为什么将宽度设置为 0dp 使文本居中吗?这种行为是否也超越了其他布局元素?
  • match_parent 在父级是 ConstraintLayout 时无效,因此您可能会得到时髦的结果。如果要限制宽度以填充可用空间,则必须使用 0dp。
猜你喜欢
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2011-04-07
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多