如何布局?使用相对布局还是线性布局?
问题描述:
我有一个Android的布局在这里挣扎,这似乎应该是这么简单,更无须欢喜,在图像中简单的线性布局结果显示:如何布局?使用相对布局还是线性布局?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:minWidth="25px"
android:minHeight="100dp">
<TextView
android:text="Last Game"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center_horizontal"
android:background="#003300" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="80dp"
android:id="@+id/imageView1"
android:layout_gravity="left"
android:layout_height="80dp" />
<TextView
android:textColor="#000000"
android:text="WR"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/textView2"
android:gravity="right"
android:paddingRight="5dp" />
<View
android:background="#000000"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:id="@+id/view1"
android:layout_gravity="center_horizontal" />
<TextView
android:textColor="#000000"
android:text="LR"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/textView3"
android:gravity="left"
android:paddingLeft="5dp" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_gravity="right"
android:layout_width="80dp"
android:id="@+id/imageView1"
android:layout_height="80dp" />
</LinearLayout>
</LinearLayout>
我想要什么,左图像位于左边距(左对齐) 右边距右边图像(右对齐) 垂直条始终居中 而WR右对齐靠近中心 LR左对齐靠近中心。
我应该使用相对布局?
答
我会的。您可以使用
android:layout_alignParentLeft="true"
和
android:layout_alignParentRight="true"
的图像,respecitvely。然后使用
android:layout_centerHorizontal="true"
为您的生产线和
android:layout_toLeftOf="id" // enter id of line
和
android:layout_toRightOf="id" // enter id of line
将您TextView
S于该线的一侧,并添加padding
需要
答
你需要的FrameLayout,适合以下布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:minWidth="25px"
android:minHeight="100dp">
<TextView
android:text="Last Game"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center_horizontal"
android:background="#003300" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="80dp"
android:id="@+id/imageView1"
android:layout_gravity="left"
android:layout_height="80dp" />
<TextView
android:textColor="#000000"
android:text="WR"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
/>
<View
android:background="#000000"
android:layout_width="1dp"
android:layout_height="80dp"
android:id="@+id/view1"
android:layout_gravity="center_horizontal" />
<TextView
android:textColor="#000000"
android:text="LR"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:layout_marginRight="25dp"
/>
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_gravity="right"
android:layout_width="80dp"
android:id="@+id/imageView1"
android:layout_height="80dp" />
</FrameLayout>
</LinearLayout>
答
的RelativeLayout您的要求最好使用 。使左图像layout_alignParentLeft = true。使中心线layout_centerHorizontal = true。使WR layout_align_toLeftOf =“行号”,并有重力=“正确的”。在另一边重复相反的方向。
LinearLayout中有五个意见和重量,我会说? – 2013-04-08 14:41:55
保持它们在不同的线性布局。根据您的需要设置填充和** android:gravity =“left”**和** right **根据您的需要。 – Numair 2013-04-08 14:45:01