Android RelativeLayout-如何定位? [英] Android RelativeLayout -- how to position?
问题描述
我想要这样的布局:
I want a Layout like this:
但是我的代码不起作用.我无法实现此版式,并且我不知道到目前为止所做的事情有什么问题.
But my code doesn't work. I can't achieve this Layout, and I don't know what is wrong with what I've done so far.
这是我到目前为止所拥有的-layout_gravity可以吗?还是需要在RelativeLayout
中设置它?
Here's what I have so far -- Is the layout_gravity OK? Or does it need to be set it in the RelativeLayout
?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/topText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_gravity="top" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/centerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_below="@id/topText">
<ImageButton
android:id="@+id/lektionBackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:layout_gravity="left"/>
<ImageView
android:id="@+id/centerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:layout_gravity="center"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lektionForwardButton"
android:src="@drawable/forward"
android:layout_gravity="right"/>
</LinearLayout>
<TextView
android:id="@+id/bottomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_below="@id/centerLayout"
android:layout_gravity="bottom" />
</RelativeLayout>
</RelativeLayout>
推荐答案
您完全不需要使用重力或内部的LinearLayout
.而是在子级上使用layout_alignParentTop
,layout_alignParentLeft
,layout_alignParentRight
,layout_alignParentBottom
,layout_centerInParent
和layout_centerVertical
.
You don't need to use gravity or the inner LinearLayout
at all for this. Instead use layout_alignParentTop
, layout_alignParentLeft
, layout_alignParentRight
, layout_alignParentBottom
, layout_centerInParent
and layout_centerVertical
on the children.
阅读 RelativeLayout教程可能对您很有用..
It might be useful for you to go through the RelativeLayout tutorial.
这篇关于Android RelativeLayout-如何定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!