一:布局管理器分类:
1.LinearLayout : 线性布局管理器,分为水平和垂直两种,只能进行单行布局。
2.FrameLayout :所有组件放在左上角,一个覆盖一个。
3.TableLayout :任意行和列的表格布局管理器,其中TableRow代表一行,可以向行中增加组件。
4.RelativeLayout:相对布局管理器,更具最近一个视图组件,或是顶层父组件来确定下一个组件的位置。
5.废除的布局管理器 :在Android 2.3.3版本之前还存在着一种绝对布局管理器(AbsoluteLayout),
此布局管理器使用X、Y轴坐标的形式排列组件。
1. 线性布局管理器
1. 通过main.xml布局配置:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > //垂直放置
......
</LinearLayout>
2. 通过Activity动态布局:
package com.LinearLayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MylLinerLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this); //定义线性布局管理器
LinearLayout.LayoutParams txt = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, //定义布局管理器参数
LinearLayout.LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL); //所有组件采用垂直方式摆放
//下面要定义显示组件的布局管理器,为了简单,本次只定义一个TextView组件
LinearLayout.LayoutParams txtView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, //定义组件的布局管理器参数
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView text1 = new TextView(this); //定义一个文本显示组件
TextView text2 = new TextView(this); //定义一个文本显示组件
text1.setLayoutParams(txtView); //配置文本显示组件
text1.setText("浙江科技学院"); //配置显示文字
text2.setText("欢迎光临!"); //配置显示文字
text1.setTextSize(20);
layout.addView(text1, txtView); // 增加组件
layout.addView(text2, txtView); // 增加组件
setContentView(layout, txt); // 增加新的布局管理器
}
}
01 线性布局管理器
最新推荐文章于 2023-02-03 20:42:35 发布