【问题标题】:How to set the same width for 3 buttons in android RelativeLayout?如何在android RelativeLayout中为3个按钮设置相同的宽度?
【发布时间】:2017-03-22 14:48:12
【问题描述】:

更新问题:我知道 LinearLayout 更好,并且已经用线性实现了它。 我有一个教育任务需要RelativeLaouyt 实现。 请不要评论线性布局,这不是我的问题的目的。

我知道在 LinearLayout 中它更容易,但我必须仅使用 RelativeLayout 来实现它。 我的 xml 文件中有 3 个按钮,希望它们位于同一行,并且宽度相同,等于屏幕的 1/3。

(我发现here 是使用“假”视图的 2 个按钮的不错解决方案。但它不适用于 3 个按钮。)

有什么想法吗? (仅限相对布局!)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="10dp">      
<Button
            android:id="@+id/bottom_left"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Left"/>
        <Button
            android:id="@+id/bottom_center"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_centerInParent="true"
            android:text="Center"/>
        <Button
            android:id="@+id/bottom_right"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_alignParentRight="true"
            android:text="Right"/>
</RelativeLayout>

【问题讨论】:

  • 您可以随时以编程方式添加它们并计算那里的宽度。
  • 虽然相对 ei 比线性布局更好。你可以做一个线性,然后给每个按钮一个均匀的权重
  • 原因只是教育目的。我需要用不同的布局实现相同的小部件。
  • 对不起,娜塔莉。我没有看到任何教育目的。如前所述,每个布局都有自己的用途。如果您仍想使用RelativeLayout,则必须编写额外的代码来在运行时处理宽度。或使用PercentRelativeLayout 并为所有按钮提供相等的宽度百分比。此外,如果它具有教育意义,您必须自己尝试,而不是在 Stack Overflow 上发布。
  • 这不是我的作业...只是想了解是否有任何方法可以使用一种布局。如果你不知道答案,你不必回答。

标签: android android-relativelayout android-layout-weight


【解决方案1】:

参考您为 2 个按钮提供的链接,您也可以使用相对布局为 3 个按钮使用相同的链接。 答案可能是,为每个视图设置 3 个不同的相对布局属性。 例如, 按钮 1:alignParentLeft true 按钮 2:centerInParent true 按钮 3:alignParentRight true

您部分正确,centerInParent 是您需要的属性。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2019-12-12
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多