5.1 . 布局管理器分类
5.2 . LinearLayout(线性布局管理器,常用)
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.LinearLayout
线性布局管理器通过布局管理器的XML文件在前面许多地方都用到了,这里就不用再详说
5.2.1.通过XML配置文件来配置
线性布局管理器:LinearLayout
5.2.2.通过程序- LinearLayout.LayoutParams来控制
现在来看看布局管理器的程序控制
布局参数通过LinearLayout.LayoutParams来完成
java.lang.Object
android.view.ViewGroup.LayoutParams
android.view.ViewGroup.MarginLayoutParams
android.widget.LinearLayout.LayoutParams
构造器:
LinearLayout.LayoutParams(Context c, AttributeSet attrs)
LinearLayout.LayoutParams(int width, int height)
LinearLayout.LayoutParams(int width, int height, float weight)
Creates a new set of layout parameters with the specified width, height and weight.
LinearLayout.LayoutParams(ViewGroup.LayoutParams p)
LinearLayout.LayoutParams(ViewGroup.MarginLayoutParams source)
LinearLayout类的常用操作方法及常量
package com.makyan.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class LinearLayoutActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);//定义线性布局管理器
//定义布局管理器的参数
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//定义显示组件的参数
LinearLayout.LayoutParams textParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setLayoutParams(textParam);//配置文本显示组件的参数
tv.setText("杨雄Android工作室");
layout.addView(tv,textParam);//增加组件
setContentView(layout,param);//增加新的布局管理器 ,注意,已经不在是 setContentView(R.layout.activity_linear_layout);
}
}
5.3 . FrameLayout(框架布局管理器):
掌握框架布局管理器的使用特点
可以在布局管理器之中使用框架布局进行排列
可以使用FrameLayout和FrameLayout.LayoutParams类在Activity程序之中动态生成布局
FrameLayout的使用和LinearLayout是一样的
使用FrameLayout进行布局:
5.3.1. 通过XML配置文件来实现
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/myimg"
android:src="@drawable/mldn_3g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/myinput"
android:text="请输入您的姓名..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按我" />
</FrameLayout>
5.3.2. 通过程序- FrameLayout.LayoutParams来实现
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
==========================================================================
java.lang.Object
android.view.ViewGroup.LayoutParams
android.view.ViewGroup.MarginLayoutParams
android.widget.FrameLayout.LayoutParams
==========================================================================
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MyFrameLayoutDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout layout = new FrameLayout(this); // 定义帧布局管理器
FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT); // 定义布局管理器的参数
FrameLayout.LayoutParams viewParam = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT); // 定义显示组件的参数
ImageView img = new ImageView(this); // 定义图片组件
img.setImageResource(R.drawable.mldn_3g); // 定义显示的图片
EditText edit = new EditText(this); // 定义文本输入