2012-04-27 57 views
15

我有一个ImageView和一个ImageButton。我将它们放在一个水平布局中彼此相邻。我正在尝试使图像在屏幕上左对齐,并且按钮右对齐。我试图设置引力,但它似乎没有什么区别。我哪里错了?Android布局左右对齐水平布局

<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> 

回答

29
<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> 
+0

使用相对布局它始终工作 – MAC 2012-04-27 17:37:59

+9

的事情是,你应该总是倾向于回答与它的方式的问题这个例子问,它可以通过使用LinearLayout通过设置weightSum属性轻松完成... – 2012-04-27 17:58:40

+1

是的,你是对的@mirroredAbstraction 但尝试给你最好的。 和LinearLayout对于真正的开发没有多大用处,我建议他使用Relative ... – MAC 2012-04-27 18:04:54

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 > 
10

使用...

<?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> 
+0

我不这会工作 – 2012-04-27 17:42:35

+0

@Agarwal,你为什么这么认为,你尝试过吗? – 2012-04-27 17:55:26

+2

它工作得很好,除此之外......这应该是正确的答案......因为问题是......如何用LinearLayout解决问题......不是用RelativeLayout提供替代方案......我也知道与RelativeLayout ...但我需要与LinearLayout ...这必须是正确的答案 – 2013-03-24 14:51:08

0

imageviewimagebuttonwrap_content并设置layout_gravity左,右相应。

0

一个RelativeLayout将解决你的问题,啪的一声,但如果你仍然坚持使用的LinearLayout(只是因为我们是坏蛋和做事困难的方式),你做这样的:

你父母的LinearLayout是填充父级,左对齐和水平方向, 并包含您的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" 
2

使用,如果你想使用的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>