【问题标题】:why the right button can't on the right of parent like left button on the left of parent为什么右键不能像父级左侧的左键一样位于父级的右侧
【发布时间】:2014-12-22 09:42:39
【问题描述】:
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="left" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="center" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="right" />
    </LinearLayout>

我知道我也可以使用Relativelayout并设置alignparentRight=true,让右键在这个父级的右边,但我只想知道,如果我使用LinearLayout,我怎样才能使右键对它的父母?任何帮助谢谢

【问题讨论】:

  • 是的,但它不起作用。我真的很想知道为什么

标签: android layout-gravity


【解决方案1】:

如果您只有三个或两个孩子,您可以使用FrameLayout 而不是LinearLayout。但是如果你想使用LinearLayout 这样做

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="left" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="center" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="right" />

</LinearLayout>

使用FrameLayout 作为父级

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:text="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="right" />

</FrameLayout>

为什么 layout_gravity="right" 不适用于 Horizo​​ntal LinearLayout explained here

对于水平线性布局,以下值是有意义的:

  • 顶部
  • 居中
  • 底部

那是因为水平 Linear Layout 的子元素是被放置的 一个接一个地水平向外。只有事情可以控制 使用 android:layout_gravity 是如何定位子视图 垂直。

对于垂直线性布局,以下值是有意义的:

  • 居中

那是因为垂直线性布局的子布局 垂直一个低于另一个。唯一可以使用的东西可以控制 android:layout_gravity 是子视图的水平定位方式。

【讨论】:

  • 谢谢,如果我像你说的那样使用 LinearLayout,没有 Layout_gravity 属性它也可以工作,因为 layout_weight.but 谢谢
  • LinearLayout 中不需要layout_gravity 只需要layout_weight
  • 是的,但我的问题是,如果我在水平 LinearLayout(android:layout_width="match_parent") 中有两个按钮,一个是 layout_gravity="left",另一个是 layout_gravity="right",为什么右键不能在其父级的右侧。我对此感到非常困惑。
  • 对于您的要求,请使用Framelayout 为什么layout_gravity 不起作用explained here. 请阅读。
【解决方案2】:

使用

 Relative layout instead of 
 linear layout. 

并使用 layout_alignParentRight 使其位于父级的右侧。

 <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />
</RelativeLayout>

【讨论】:

  • 他好像已经知道那个方法了。他只是想用线性布局尝试同样的方法。请完整阅读问题!
  • 谢谢,我知道。但我只想知道,如果我使用LinearLayout,我怎样才能在它的父级右侧制作正确的按钮?
【解决方案3】:

将中心按钮放在权重为 1 的 RelativeLayout 中,以便中心布局占据整个区域。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />
    <RelativeLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="center" />
    </RelativeLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

两个按钮

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />
    <RelativeLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </RelativeLayout>
</LinearLayout>

【讨论】:

  • 谢谢,它有效。但是如果我在 LinearLayout 中只有两个孩子,如何制作它们,一个在父级的左侧,另一个在父级的右侧。如何设置 layout_gravity ?
  • 如果我将 RelativeLayout 设置为消失,它将无法正常工作
  • 对于 2 个按钮,您可以删除第三个按钮并将 android:gravity="right" 放在 RelativeLayout
【解决方案4】:

为什么不使用权重,

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="left"
        android:layout_weight="33"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="left" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="34"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="33"
        android:gravity="right"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </LinearLayout>
</LinearLayout>

如果你有两个按钮

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="left"
        android:layout_weight="50"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="left" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:gravity="right"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right" />
    </LinearLayout>
</LinearLayout>

注意:实现这种方式需要添加许多布局,当应用程序非常繁重时会出现问题。始终建议使用相对布局。

【讨论】:

  • 谢谢,但我也很困惑如何使用 layout_gravity?为什么我在右键上设置了 layout_gravity="right" 但它不起作用。
  • 坏主意。如果 single 可以完成这项工作,我们不应该使用太多布局。布局的数量会影响应用程序的性能,如果深度过多,甚至会导致崩溃。改用相对布局。使用线性布局有什么具体要求吗??
  • @Rohit5k2 感谢您指出这一点。我将在我的答案上添加注释。但答案是向 OP 展示解决方法。 OP 已经知道使用相对布局来实现。
  • 是的,这就是为什么我问@lightman1988 他/她是否有任何特定的想法迫使他/她使用线性布局。 :)
  • 谢谢,我只想知道为什么 layout_weight="right" 不起作用。如果我想要按钮右侧的右键。如果我有一个孩子,它可以工作,但为什么要两个或更多不能
【解决方案5】:

如果您不希望 FrameLayoutRelativeLayout 造成潜在的重叠原因并且仍然想使用 wrap_content 宽度,如果您在 RelativeLayout 中正确对齐视图以避免重叠,则可以使用另一种方法:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

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

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" />

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

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" />

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

</LinearLayout> 

这种方法很常见,并且被 appcompat 库中的AlertDialog 使用(至少我上次检查时是这样)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 2016-11-03
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多