【问题标题】:Make button width equal DIFFERENT text width (not the button's text) on Android在Android上使按钮宽度等于不同的文本宽度(不是按钮的文本)
【发布时间】:2014-08-28 10:38:17
【问题描述】:

现在我的登录按钮比我的应用名称文本更宽,因为它使用相对布局的固定填充,但我希望它自动加宽以与应用名称文本的左右边缘对齐。有没有办法以编程方式做到这一点?

我希望它在所有设备上看起来都一样。恐怕如果我只是对自己的边距进行硬编码,它在我的设备上看起来会是正确的,但如果它们以不同的方式呈现字体,则在其他设备上可能看起来不正确。

谢谢!

layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@drawable/back_image"
android:padding="20dp" >

    <TextView
    android:id="@+id/AppNameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/margin_top"
    android:fontFamily="Arial"
    android:text="@string/app_title"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size" />

    <com.timey.widget.Button
    android:id="@+id/signInButton"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:background="@color/green"
    android:fontFamily="Arial"
    android:text="@string/sign_in"
    android:textColor="@color/white"
    android:textSize="30sp" />

</RelativeLayout>

带有红色虚线的样机说明我希望应用名称的两侧与登录按钮的两侧对齐:

【问题讨论】:

  • 尝试使用线性布局
  • 如何使它们具有相同的宽度?
  • 等着给你答案

标签: android


【解决方案1】:

我不完全确定这是否会有所帮助,但让我试一试。

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="App\nTitle" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/title"
        android:layout_alignRight="@id/title"
        android:layout_below="@id/title"
        android:text="Log In" />

</RelativeLayout>

但是,按钮与文本视图一样宽,它可能不够宽,无法显示自己的文本。

【讨论】:

  • 想解释一下否决票?当我在我的机器上测试它时它工作正常
  • 是的,它运行良好,@CChi。 +1 来自我。虽然它与我的相似;-)。为什么要投反对票?
  • 结果可能相似,但我认为 relativelayout 是一个要求。
  • 我只需要添加这些:android:layout_alignLeft="@id/title" 和 android:layout_alignRight="@id/title",它就可以工作了。谢谢CChi!
【解决方案2】:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button2" />

</LinearLayout>

第一个按钮将占用所需的空间。然后下一个按钮(或您的情况)将占用与其父级(线性布局)一样多的空间,另一侧将包装内容,即第一个按钮的宽度。所以两个按钮都将采用相同的宽度。您可以将其用于任何其他组件。

更新1

Key to the success
1. A container with wrap_content
2. First component should have wrap_content
3. Second component should have match_content

更新2 对于 RelativeLayout 使用 layout_alignLeft="@+id/button1" 和 layout_alignRight="@+id/button1" 它将与第一个组件的左右对齐,即此处的 button1。

【讨论】:

  • 我没有使用 2 个按钮。我正在尝试排列文本和按钮。
  • 该概念适用于上述答案中提到的任何组件
  • 我需要使用RelativeLayout,而不是LinearLayout。
  • 要使用 RelativeLayout 使用 layout_alignLeft="@+id/button1" 和 layout_alignRight="@+id/button1"
  • 它将与第一个组件的左右对齐,即此处的 button1。希望它对你有用。让我知道它是否有效。祝你好运
猜你喜欢
  • 2020-05-29
  • 2015-02-27
  • 1970-01-01
  • 2015-06-24
  • 2014-05-07
  • 1970-01-01
  • 2017-07-21
  • 2016-02-08
  • 2013-03-28
相关资源
最近更新 更多