我有Horizontal Layout
,其中有一些观点。我想要一些从左边开始,其他人从右边开始,但我无法做到。我尝试了几种Gravity
配置,但它们没有做任何事情。
我的情况就是这样:
我希望Flag位于右侧,时间位于左侧,如箭头所示。我稍后会添加一些标志。
有人可以帮我解决这个问题吗?谢谢:D
编辑:
到目前为止XML:
<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>
答案 0 :(得分: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>
结果:
答案 1 :(得分:1)
android:layout_gravity
在与LinearLayout方向相反的方向上工作 - vertical LinearLayout
的子项可以使用android:layout_gravity来控制它们的位置horizontally (left or right)
,但不能垂直。同样地,horizontal LinearLayout
的孩子可以使用android:layout_gravity来控制他们的位置vertically (top or bottom)
但不能水平控制。{1}}当您使用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>
答案 2 :(得分: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>