从零开始学android<linearlayout线性布局.十三.>

布局其实可以说就是一个盒子,一个装着其他组件的盒子。

所谓线性布局就是组件在水平方向或者竖直方向依次排列的布局

<LinearLayout //线性布局管理器
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" //所有组件采用垂直方式由上向下排列
android:layout_width="fill_parent"// 此布局管理器将填充整个屏幕宽度
android:layout_height="fill_parent"> // 此布局管理器将填充整个屏幕高度

…………………………………………………………毫无美感的分割线………………………………………………………… 

下面我们进行布局管理的配置

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >//垂直摆放组件,如果设置为horizontal则为水平放置  
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="我是第一行" />  
  11.   
  12.     <EditText  
  13.         android:id="@+id/editText1"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:ems="10"  
  17.         android:text="我是第二行" >  
  18.   
  19.         <requestFocus />  
  20.     </EditText>  
  21.   
  22.     <Button  
  23.         android:id="@+id/button1"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="我是第三行" />  
  27.   
  28.     <CheckBox  
  29.         android:id="@+id/checkBox1"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:text="我是第四行" />  
  33.   
  34.     <RadioGroup  
  35.         android:id="@+id/radioGroup1"  
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content" >  
  38.   
  39.         <RadioButton  
  40.             android:id="@+id/radio0"  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="wrap_content"  
  43.             android:checked="true"  
  44.             android:text="我是第五行" />  
  45.   
  46.         <RadioButton  
  47.             android:id="@+id/radio1"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="我是第六行" />  
  51.   
  52.         <RadioButton  
  53.             android:id="@+id/radio2"  
  54.             android:layout_width="wrap_content"  
  55.             android:layout_height="wrap_content"  
  56.             android:text="我是第七行" />  
  57.     </RadioGroup>  
  58.   
  59. </LinearLayout>  
  60. </span>  
JAVA文件不用设置

  1. <span style="font-size:18px;">package com.example.linearlayout13;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.linearlayout);  
  13.     }  
  14.       
  15. }  
  16. </span>  



大家可以看到,上面的组件都是水平放置,是因为:

  1. <span style="font-size:18px;"> android:orientation="vertical" >//垂直摆放组件,如果设置为horizontal则为水平放置</span>  
大家可以在下面自己试试。


当然也可以在JAVA程序中进行设置。

…………………………………………………………毫无美感的分割线…………………………………………………………

LinearLayout组件类的继承结构:
java.lang.Object
  ↳ android.view.View
  ↳ android.view.ViewGroup
     ↳ android.widget.LinearLayout
No.
方法及常量
类型
描述
1
public static final int HORIZONTAL
常量
设置水平对齐
2
public static final int VERTICAL
常量
设置垂直对齐
3
public LinearLayout(Context context)
构造
创建LinearLayout类的对象
4
public void addView(View child, ViewGroup.LayoutParams params)
普通
增加组件并且指定布局参数
5
public void addView(View child)
普通
增加组件
6
protected void onDraw(Canvas canvas)
普通
用于图形绘制的方法
7
public void setOrientation(int orientation)
普通
设置对齐方式

LinearLayout.LayoutParams类提供了以下一个构造方法:
public LinearLayout.LayoutParams (int width, int height)
常用布局参数:
public static final int FILL_PARENT
public static final int WRAP_CONTENT

配置文件默认

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.      
  8. </LinearLayout>  
  9. </span>  

JAVA文件配置
  1. <span style="font-size:18px;">package com.example.linearlayout13;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.ViewGroup;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         LinearLayout layout = new LinearLayout(this);//创建现行布局  
  16.         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  17.                 ViewGroup.LayoutParams.FILL_PARENT,  
  18.                 ViewGroup.LayoutParams.FILL_PARENT);//设置现行布局的配置  
  19.         layout.setOrientation(LinearLayout.VERTICAL);//设置现行布局的对齐方式  
  20.         LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(  
  21.                 ViewGroup.LayoutParams.WRAP_CONTENT,  
  22.                 ViewGroup.LayoutParams.WRAP_CONTENT);//设置文本的对齐方式  
  23.         for (int i = 0; i < 5; i++) {  
  24.             TextView text = new TextView(this);//实例化TextView  
  25.             text.setLayoutParams(textParams);//设置默认配置方式  
  26.             text.setText("第" + i + "行");  
  27.             text.setTextSize(20);//设置文本大小  
  28.             layout.addView(text, textParams);//将文本组件添加到线性布局  
  29.   
  30.         }  
  31.         super.addContentView(layout, layoutParams);//设置线性布局  
  32.   
  33.     }  
  34.   
  35. }</span>  

大家也可以根据自己想法设置想要的组件,我比较懒就用循环输出了。

下节预报:

框架布局管理器:FrameLayout

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值