Android布局:水平布局中左右对齐

22
我有一个 ImageView 和一个 ImageButton,它们在水平布局中彼此相邻。我想让图片左对齐,按钮右对齐。我尝试设置重力,但好像没有起作用。我错在哪里了?

我有一个 ImageView 和一个 ImageButton,它们在水平布局中彼此相邻。我想让图片左对齐,按钮右对齐。我尝试设置重力,但好像没有起作用。我该怎么解决?

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:gravity="left"
        android:src="@drawable/short_logo" />

    <ImageButton
        android:id="@+id/terminateSeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:background="@null"
        android:src="@drawable/unregister_icon" />
</LinearLayout>
7个回答

33
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

10
重点是你应该根据问题的提出方式来回答问题,可以通过设置LinearLayout的weightSum属性来轻松实现... - Arif Nadeem
1
是的,@mirroredAbstraction,你说得对,但是尽力而为。 LinearLayout在实际开发中并不是很有用,所以我建议他使用相对布局。 - MAC
LinearLayout非常适合那些不想在自定义RelativeLayout中重新实现onMeasure和onLayout功能的情况。 - samus

12

Use...

 <?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:weightSum="1.0" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginRight="80dp"
    android:layout_weight="0.5"
    android:gravity="left"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="right"
    android:layout_marginLeft="80dp"
    android:background="@null"
    android:src="@drawable/ic_launcher" />
</LinearLayout>

@Agarwal,你为什么这样认为,你试过了吗? - Arif Nadeem
3
它的效果非常好,此外...这应该是正确的答案...因为问题是...如何解决 LinearLayout 的问题...而不是提供 RelativeLayout 的替代方案...我也知道用 RelativeLayout...但我需要用 LinearLayout...这一定是正确的答案。 - Dany's
这绝对是问题的正确答案!相对布局只是一种替代方案。只有一个问题,为什么要使用 android:layout_marginLeft="80dp"? - Igor Čordaš

3
如果您想使用LinearLayout而不使用权重,请使用此示例。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="right"
        />
    </LinearLayout>

0
<RelativeLayout 
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"

    android:layout_alignParentLeft="true"
    android:src="@drawable/short_logo" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@null"
    android:src="@drawable/unregister_icon" />
</RelativeLayout >

0

RelativeLayout 可以轻松解决您的问题,但如果您仍然坚持使用 LinearLayout(只是因为我们很酷,喜欢用困难的方式完成任务),则可以按照以下方式进行:

您的父 LinearLayout 为 fill-parent、左对齐和水平方向, 其中包含您的 ImageView 和另一个 LinearLayout。

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"

第二个LinearLayout是填充父布局,右对齐,水平方向,并包含您的ImageButton。

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"

0

imageviewimagebuttonwrap_content 属性生效,并将 layout_gravity 分别设置为左和右。


0

使用 layout_weightgravity 的另一种 LinearLayout 方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BUTTON 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BUTTON 2" />
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接