Android详细教程(基础篇):五、布局管理器详解

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); // 定义文本输入
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值