Android 基本布局
来源:互联网 发布:海信ip906h安装软件 编辑:程序博客网 时间:2024/06/05 09:02
本文主要介绍几种基本布局,具体操作看代码。
一、RelativeAndLinearLayout
1.RelativeAndLinearActivity.java
public class RelativeAndLinearActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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"?><RelativeLayoutandroid:id="@+id/left"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/view1"android:layout_width="fill_parent"android:layout_height="50px"android:background="#cc3399"android:text="第a组第a项"/><TextViewandroid: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"?><RelativeLayoutandroid:id="@+id/right"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/right_view1"android:layout_width="fill_parent"android:layout_height="50px"android:background="#ffff00"android:text="第b组第a项"/><TextViewandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.relative_layout);}}
2.relative_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#cc3399"android:padding="10dip"><TextViewandroid:id="@+id/label"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="请输入用户名:"/><!--这个EditText放置在上边id为label的TextView的下边--><EditTextandroid: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--><Buttonandroid: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="取消"/><!--确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平--><Buttonandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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"/><EditTextandroid:id="@+id/username"android:padding="3dip"android:scrollHorizontally="true"/></TableRow><TableRow><TextViewandroid:text="密码:"android:textStyle="bold"android:gravity="right"android:padding="3dip"/><EditTextandroid: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{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.frame_layout);}}
2.布局文件frame_layout.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/frameLayout_"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid: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按钮将位于界面的左部。
0 0
- android 基本布局
- android基本布局
- Android基本布局学习
- Android-基本布局
- Android 基本布局
- Android基本布局
- Android基本布局解析
- Android 基本布局
- Android基本布局总结
- Android 基本布局
- Android基本布局案例
- Android基本布局-RelativeLayout
- Android基本布局-FrameLayout
- android基本布局模式
- android五大基本布局
- android基本布局之LinearLayout
- android-四种基本布局
- Android 基本布局(1)LinearLayout
- uva 11275
- UVA1605-Building for UN
- Contiki学习笔记——RPL协议详解
- nodejs使用笔记
- 从决策树学习谈到贝叶斯分类算法、EM、HMM
- Android 基本布局
- 编译报错:Undefined symbols for architecture x86_64
- 【特征工程】特征工程技术与方法
- Gitolite 安装
- sprintf
- Invert Binary Tree
- android tcpdump 抓取网络通讯包
- JAVA基本类型和引用类型
- linux panic 和 bug_on