本文主要介绍几种基本布局,具体操作看代码。
一、RelativeAndLinearLayout
1.RelativeAndLinearActivity.java
public class RelativeAndLinearActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//创建线性布局LinearLayout对象
LinearLayout layoutMain=new LinearLayout(this);
//设置水平方向
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
/**
* LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!
* 而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
*/
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft=(RelativeLayout)inflater.inflate(R.layout.relative_linear_left, null);
RelativeLayout layoutRight=(RelativeLayout)inflater.inflate(R.layout.relative_linear_right, null);
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight,layoutParams);
}
}
2.布局文件relative_linear_left.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/left"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#cc3399"
android:text="第a组第a项"
/>
<TextView
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#ffff00"
android:layout_below="@id/view1"
android:text="第a组第b项"
/>
</RelativeLayout>
3.布局文件relative_linear_right.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/right"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/right_view1"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#ffff00"
android:text="第b组第a项"
/>
<TextView
android:id="@+id/right_view2"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#cc3399"
android:layout_below="@id/right_view1"
android:text="第b组第b项"
/>
</RelativeLayout>
4. 注意:需要在AndroidManifest.xml注册相应Activity.
二、RelativeLayout
1.RelativeLayoutActivity
public class RelativeLayoutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.relative_layout);
}
}
2.relative_layout.xml
<?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="wrap_content"
android:background="#cc3399"
android:padding="10dip"
>
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入用户名:"
/>
<!--
这个EditText放置在上边id为label的TextView的下边
-->
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"
/>
<!--
取消按钮和容器的右边齐平,并且设置左边的边距为10dip
-->
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="取消"
/>
<!--
确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平
-->
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancel"
android:layout_alignTop="@id/cancel"
android:text="确定"
/>
</RelativeLayout>
3. 注意:需要在AndroidManifest.xml注册相应Activity.
三、TableLayout
1.TableLayoutActivity.java
public class TableLayoutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
}
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:text="用户名:"
android:textStyle="bold"
android:gravity="right"
android:padding="3dip"
/>
<EditText
android:id="@+id/username"
android:padding="3dip"
android:scrollHorizontally="true"
/>
</TableRow>
<TableRow>
<TextView
android:text="密码:"
android:textStyle="bold"
android:gravity="right"
android:padding="3dip"
/>
<EditText
android:id="@+id/password"
android:password="true"
android:padding="3dip"
android:scrollHorizontally="true"
/>
</TableRow>
</TableLayout>
3. 注意:需要在AndroidManifest.xml注册相应Activity.
四、FrameLayout
1.FrameLayoutActivity.java
public class FrameLayoutActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_layout);
}
}
2.布局文件frame_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout_"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/photo"
android:src="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
3. 注意:需要在AndroidManifest.xml注册相应Activity.
补充布局属性知识点:
android:layout_gravity和android:gravity的区别
android:gravity:
这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性,
android:gravity="left"和android:text="提交",这时Button上的文字“提交”将会位于Button的左部。
android:layout_gravity:
这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。同样,当我们在Button按钮控件中设置android:layout_gravity="left"属性时,表示该Button按钮将位于界面的左部。