Android学习之旅——android中布局管理器的总结
来源:互联网 发布:淘宝网天猫大码女装 编辑:程序博客网 时间:2024/05/19 02:22
对于一个应用来说,好的交互性界面会带来不一样的用户体验,良好的用户体验会给你的应用带来用户群,这是每一个开发者都愿意看到的。对于Android开发者来说也是这样,如何将界面做的更好,一个很基本的要求就是深入研究布局管理器。下面我们将Android中的布局管理器做一个总结。
Android中提供了五种布局管理器,分别是线性布局管理器,框架布局管理器,表格布局管理器,相对布局管理器和绝对定位布局管理器。其中绝对定位布局管理器在Android2.3.3版本后被废除了,所以我们只总结前四种布局管理器。
(一)线性布局管理器:LinearLayout
使用线性布局管理器管理的组件要么水平排列,要么垂直排列,两种方式的排列都是在一条线上,当排列的组件超过设备的宽度后组件可能不可见,LinearLayout有两种使用方式,一种是通过配置文件进行定义,一种是通过程序来操作
1,使用配置文件
使用配置文件定义的布局管理器代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LinearLayoutDemoActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
2,通过程序直接生成布局管理器
使用程序直接生成布局管理器需要定义布局管理器以及托管在管理器的组件的布局参数,此参数通过LinearLayout.LayoutParams来实现,具体的代码如下;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this) ;
LinearLayout.LayoutParams layoutparam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT) ;
LinearLayout.LayoutParams txtparam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
TextView text = new TextView(this) ;
layout.setOrientation(LinearLayout.HORIZONTAL) ;
layout.addView(text, txtparam) ;
super.setContentView(layout, layoutparam) ;
}
(二)框架布局管理器:FrameLayout
通过框架布局管理器进行组件的布局,就是在屏幕的左上角开辟一片区域用来布局组件,采用这种方法所有的组件层叠显示,,也就是说组件会出现叠加的情况。
FrameLayout的生成方式与LinearLayout类似,也有两种生成方式,一种是通过配置文件生成,一种是通过程序生成。
1,通过配置文件生成,代码如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="press me"
>
</Button>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
>
</ImageView>
<EditText
android:id="@+id/edit"
android:text="please input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText>
</FrameLayout>
2.通过程序生成如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout framelayout = new FrameLayout(this) ;
FrameLayout.LayoutParams layoutparam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
FrameLayout.LayoutParams viewLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
Button button = new Button(this) ;
button.setText("press me") ;
ImageView image = new ImageView(this) ;
image.setImageResource(R.drawable.ic_launcher) ;
EditText edit = new EditText(this) ;
edit.setText("input data") ;
framelayout.addView(button, viewLayout) ;
framelayout.addView(image, viewLayout) ;
framelayout.addView(edit, viewLayout) ;
super.setContentView(framelayout,layoutparam) ;
}
(三)表格布局管理器:TableLayout
在Android中可以通过表格的形式对界面进行管理,这要通过TableLayout来实现。在TableLayout中要通过TableRow来对界面的行进行控制,之后所有的组件都要在TableRow中增加。TableLayout的使用方式也有两种,一种通过配置文件来实现,一种是通过程序来生成。
1.通过配置文件来生成表格布局管理器,代码如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow>
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="please input data"
>
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="search"
>
</Button>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="please choose sex of yourself"
>
</TextView>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/male"
>
<RadioButton
android:id="@+id/male"
android:text="male"
/>
<RadioButton
android:id="@+id/female"
android:text="female"
/>
</RadioGroup>
</TableRow>
</TableLayout>
(四)相对布局管理器:RelativeLayout
相对布局管理器是指参考某一控件对组件进行摆放,可以通过控制将组件摆放在一个指定的组件的上下左右等位置。
1. 通过配置文件生成相对布局管理器的代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text01"
android:text="hello"
>
</TextView>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me"
android:layout_below="@id/text02"
>
</Button>
</RelativeLayout>
2。通过程序生成相对布局管理器,代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative_layout_demo);
RelativeLayout relative = (RelativeLayout)super.findViewById(R.id.relativelayout) ;
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
param.addRule(RelativeLayout.ABOVE,R.id.button) ;
EditText edit = new EditText(this) ;
relative.addView(edit,param) ;
}
还有一种绝对定位布局管理器由于现在不适用或使用的较少,所以不对其做总结。
- Android学习之旅——android中布局管理器的总结
- android布局管理器学习
- Android 布局管理器学习
- Android的6种布局管理器总结
- Android常用布局管理器总结
- Android入门学习(二)——布局管理器
- Android学习6--布局管理器
- 一步一步学android之布局管理器——LinearLayout
- 一步一步学android之布局管理器——FrameLayout
- 一步一步学android之布局管理器——TableLayout
- 一步一步学android之布局管理器——RelativeLayout
- 一步一步学android之布局管理器——AbsoluteLayout
- Android学习系列(二)布局管理器之线性布局的3种实现方式
- android布局管理器之TableLayout
- Android 基础之布局管理器
- Android学习之路--基础控件<布局管理器之LinearLayout>
- android学习布局管理器的一些心得——基础篇
- android学习布局管理器的一些心得2——基础篇
- 周鸿祎:混日子,会让你失去竞争力
- dedeCMS织梦留言板调用网站head的方法
- Oracle DUL Reference document
- js的事件深入理解
- Matrix 中的post 和pre 和set 方法的区别 以及Canvas中的方法
- Android学习之旅——android中布局管理器的总结
- 回收站的东西删除了如何恢复
- Android中View的焦点事件整理
- POJ 2795 (LA 3516) Exploring Pyramids 解题报告(递推)
- 第十三章 正则表达式
- 如何设置让我们的无线路由器更安全
- 【ObjC那点儿事儿】变量作用域
- java 数字转换为字符串,长度不够前面补0
- 碎片化的数据大量产生,需要我们不断提高“数据能力