[英]Android - Place oposite views in a Layout
我有一个Horizontal Layout
,里面有一些视图。 我想让某些人从左边开始,而另一些人从右边开始,但是我无法做到这一点。 我尝试了几种Gravity
配置,但是它们什么也没做。
我就是这种情况:
我希望标志如箭头所指,在右边,时间在左边。 稍后我将添加更多标志。
有人可以帮我吗? 感谢: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>
代替使用水平布局,而使用相对布局
例:
<?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>
结果:
android:layout_gravity
工作方向与LinearLayout的方向相反- 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
。 android:layout_gravity
您的目的,最好使用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>
RelativeLayout是一个很好的答案。 但是,如果您真的想使用LinearLayout进行此操作,请尝试在中间放置一个空的TextView,宽度为0,权重为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.