【问题标题】:Right alignment of button in linear layout in Android with button side by sideAndroid中线性布局中的按钮右对齐与按钮并排
【发布时间】:2019-05-15 05:28:14
【问题描述】:

我想在线性布局中对齐视图右侧的按钮。 这导致按钮在左侧大小上,我尝试了相对布局,因为我导致一个按钮在左侧,另一个在右侧。 我希望两个按钮并排放置,间隔为 10。

请帮忙。

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


                <ImageButton
                    android:background="@null"
                    android:id="@+id/reply_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:src="@drawable/reply"
                    android:layout_gravity="left"/>

                <Button
                    android:id="@+id/readmore_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@null"
                    android:text="Read more.."
                    android:textAllCaps="false"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />
            </LinearLayout>

【问题讨论】:

  • 能把LinearLayout的全部代码放上来吗?
  • LinearLayoutlayout_gravity 中指定的孩子必须始终在正交轴的方向上。也就是说,对于水平的LinearLayout,它可以是topcenter(或center_vertical)或bottom。见stackoverflow.com/questions/4783896/…

标签: android android-linearlayout android-relativelayout


【解决方案1】:

您可以将android:layout_weight="2" 设置为您的容器,并为每个子设置android:layout_weight="1"android:layout_width="0dp",如下所示:

<?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="horizontal"
  android:layout_weight="2">

<ImageButton
    android:background="@null"
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_weight="1"
    android:layout_gravity="left"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="@null"
    android:text="Read more.."
    android:textAllCaps="false"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="14sp" />
</LinearLayout>

但是如果您之前已经开发过一些 iOS 应用程序,正如您的名字所暗示的那样,您可能会发现使用 ConstarintLayout 更容易 - 一种适合所有屏幕尺寸的布局(它非常类似于 ios 中的拖放编辑器我只是不记得它的名字)

这是一个使用ConstraintLayout的示例:

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button2"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

根据您的要求进行编辑:

<?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="horizontal"
  android:layout_weight="3">

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="left"
    android:layout_gravity="start"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:text="Read more.." 
    android:layout_weight="1"
    android:textSize="14sp" />

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

【讨论】:

  • 感谢您的回复。我使用了第一个选项,但它并没有对齐两个按钮。它只允许对齐 1 个按钮。你能再检查一次吗
  • “它只允许对齐 1 个按钮” - 这不是很清楚,帮助我更好地理解你 - 你能否添加一个屏幕截图,说明它现在的外观以及你希望它的外观?
  • 我已经编辑了我的问题并添加了有问题的输出图像
  • 所以你想要 3 个按钮在 1 行?每个按钮都会占用相同的空间?
  • @MikeM。感谢您添加此信息,通常我使用约束布局,所以我不知道。是的,我的意思是weightSum,我的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2016-09-26
  • 2015-08-11
  • 1970-01-01
  • 2014-10-24
  • 2011-12-02
  • 1970-01-01
相关资源
最近更新 更多