0

I have a Horizontal Layout and I have some views inside it. I want some to start from the left and others to start in the right, but I can't manage to do it. I tried several Gravity configurations but they don't do anything.

That's the case I have:

enter image description here

I want the Flag to be in the right and the Time to be in the left, as pointed by the arrows. I will add some more flags later.

Could anyone help me out with this? Thanks :D

EDIT:

XML so far:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:id="@+id/hlTopBar"
        android:background="#e6262626"
        android:gravity="center_vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/default_time_date_string"
            android:id="@+id/tvTime"
            android:textStyle="bold"
            android:paddingLeft="10dp"
            android:gravity="left" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibUSA"
            android:src="@drawable/united_states_flag"
            android:background="@android:color/transparent"
            android:adjustViewBounds="true" />
    </LinearLayout>
4

3 回答 3

1

android:layout_gravity在与 LinearLayout 方向相反的方向上工作 - a 的子级vertical LinearLayout可以使用 android:layout_gravity 来控制它们的定位horizontally (left or right),但不能垂直。同样的方式,孩子们horizontal LinearLayout可以使用 android:layout_gravity 来控制他们的位置vertically (top or bottom),但不能水平。当您使用Horizontal LinearLayout时,您也可以使用android:layout_gravity定位孩子top or bottom。为了您的目的,最好使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView
        .........
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        />

    <ImageView
        .......
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>
于 2014-06-13T04:33:40.637 回答
1

不要使用水平布局,而是使用相对布局

例子:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Time" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Image" />

</RelativeLayout>

结果:

在此处输入图像描述

于 2014-06-13T03:24:31.997 回答
1

RelativeLayout 是一个很好的答案。然而,如果你真的想用 LinearLayout 来做,试着在中间放一个空的 TextView,width=0 和 weight=1。

这个空视图将自动尝试填充,但其他视图没有占用太多空间。

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:id="@+id/hlTopBar"
        android:background="#e6262626"
        android:gravity="center_vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/default_time_date_string"
            android:id="@+id/tvTime"
            android:textStyle="bold"
            android:paddingLeft="10dp" />

        <TextView
            android:id="@+id/spacer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibUSA"
            android:src="@drawable/united_states_flag"
            android:background="@android:color/transparent"
            android:adjustViewBounds="true" />
    </LinearLayout>
于 2014-06-13T05:13:31.580 回答