在android的布局中有四种布局,分别是:

 1、LinearLayout   线性布局管理器,分为水平和垂直两种,只能进行单行布局。

2、FrameLayout    所有组件放在左上角, 一个覆盖一个。

3、TableLayout   任意行和列的表格布局管理器,其中TableRow代表一行,可以在行中增加组件。

4、RelativeLayout   相对布局管理器,根据最近的一个视图组件或顶级的父组件来确定下一个组件的位置。

那好吧,先从LinearLayout布局开始吧

先看效果:

第一张效果图是通过layout布局文件直接生成的

android之Layout(一)_第1张图片

第二张图片是通过代码动态生成的

android之Layout(一)_第2张图片

首先通过布局文件直接生成,这种比较简单,在layout中的xml文件中

 

            
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical" > //设置布局管理器中的组件的排列方式是垂直的,当然还有水平的布局
  5.  
  6.     <Button 
  7.         android:id="@+id/but1" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:background="#00ffff" //设置Button的背景色
  11.         android:textColor="#ffffff"  //设置Button的字体颜色
  12.         android:text="linearlayout1"   //内容
  13.         android:textSize="30dp"/>  //字体大小
  14.  
  15.    <Button 
  16.         android:id="@+id/but2" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="wrap_content" 
  19.         android:background="#ff00ff" 
  20.         android:textColor="#ffffff" 
  21.         android:text="linearlayout2"   
  22.         android:textSize="30dp"/> 
  23.    <Button 
  24.         android:id="@+id/but3" 
  25.         android:layout_width="fill_parent" 
  26.         android:layout_height="wrap_content" 
  27.         android:background="#ffff00" 
  28.         android:textColor="#ffffff" 
  29.         android:text="linearlayout3" 
  30.         android:textSize="30dp" /> 
  31.   <Button 
  32.         android:id="@+id/but4" 
  33.         android:layout_width="fill_parent" 
  34.         android:layout_height="wrap_content" 
  35.         android:background="#0000ff" 
  36.         android:textColor="#ffffff" 
  37.         android:text="linearlayout4"   
  38.         android:textSize="30dp"/> 
  39.   <Button 
  40.         android:id="@+id/but5" 
  41.         android:layout_width="fill_parent" 
  42.         android:layout_height="wrap_content" 
  43.         android:background="#00ff00" 
  44.         android:textColor="#ffffff" 
  45.         android:text="linearlayout5"   
  46.         android:textSize="30dp"/> 
  47.   <Button 
  48.         android:id="@+id/but6" 
  49.         android:layout_width="fill_parent" 
  50.         android:layout_height="wrap_content" 
  51.         android:background="#ff0000" 
  52.         android:textColor="#ffffff" 
  53.         android:text="linearlayout6"   
  54.         android:textSize="30dp"/> 
  55.   <Button 
  56.         android:id="@+id/but7" 
  57.         android:layout_width="fill_parent" 
  58.         android:layout_height="wrap_content" 
  59.         android:background="#333333" 
  60.         android:textColor="#ffffff" 
  61.         android:text="linearlayout7"   
  62.         android:textSize="30dp"/> 
  63.  
  64.   <ImageView 
  65.       android:id="@+id/p_w_picpathview" 
  66.       android:layout_width="fill_parent" 
  67.       android:layout_height="90dp" 
  68.       android:src="@drawable/cocos2d" /> 
  69.           
  70. LinearLayout> 

通过布局文件生成的只需要这个xml文件就可以了,是不是很简单,再看看通过代码动态的来生成的方式

 

            
  1. package com.example.linearlayoutproject2;  
  2.  
  3.  
  4.  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Color;  
  10. import android.view.Menu;  
  11. import android.view.ViewGroup;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14. import android.widget.LinearLayout;  
  15.  
  16. public class MainActivity extends Activity {  
  17.       
  18.     private Button redButton,greenButton,blueButton;  
  19.     private ImageView mImageView;  
  20.     @Override 
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         //定义一个布局管理器  
  24.         LinearLayout layout = new LinearLayout(MainActivity.this);  
  25.         //设置布局管理器的参数LinearLayout 是ViewGroup的子类  
  26.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
  27.                 ViewGroup.LayoutParams.FILL_PARENT,  
  28.                 ViewGroup.LayoutParams.FILL_PARENT);  
  29.         //设置布局管理器中的组件的布局方式  
  30.         LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(  
  31.                 ViewGroup.LayoutParams.FILL_PARENT,   
  32.                 ViewGroup.LayoutParams.WRAP_CONTENT);  
  33.         //设置布局管理器中的组件的布局方式是垂直方式  
  34.         layout.setOrientation(LinearLayout.VERTICAL);  
  35.           
  36.         redButton = new Button(MainActivity.this);//构造一个Button  
  37.         redButton.setBackgroundColor(Color.RED);   //设置背景色  
  38.         redButton.setText("LinearLayout1");      //设置内容  
  39.         redButton.setTextSize(30);                 //设置字体大小  
  40.         redButton.setTextColor(Color.WHITE);       //设置字体颜色  
  41.           
  42.           
  43.         greenButton = new Button(MainActivity.this);  
  44.         greenButton.setBackgroundColor(Color.GREEN);  
  45.         greenButton.setText("LinearLayout2");  
  46.         greenButton.setTextSize(30);  
  47.         greenButton.setTextColor(Color.WHITE);  
  48.           
  49.         blueButton = new Button(MainActivity.this);  
  50.         blueButton.setBackgroundColor(Color.BLUE);  
  51.         blueButton.setText("LinearLayout3");  
  52.         blueButton.setTextSize(30);  
  53.         blueButton.setTextColor(Color.WHITE);  
  54.           
  55.         mImageView = new ImageView(MainActivity.this);//构造一个ImageView  
  56.         //从sdcard中图片一张图片  
  57.         mImageView.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/cocos2d.jpg"));   
  58.           
  59.         //添加我们的组件  
  60.         layout.addView(redButton,buttonParams);  
  61.         layout.addView(greenButton,buttonParams);  
  62.         layout.addView(blueButton,buttonParams);  
  63.         layout.addView(mImageView,buttonParams);  
  64.         //最后设置我们的布局管理器  
  65.         super.setContentView(layout,params);  
  66.     }  
  67.  
  68.     @Override 
  69.     public boolean onCreateOptionsMenu(Menu menu) {  
  70.         // Inflate the menu; this adds items to the action bar if it is present.  
  71.         getMenuInflater().inflate(R.menu.main, menu);  
  72.         return true;  
  73.     }  
  74.  
  75. }  

通过代码实现也就这一个Activaty.java文件,两者的实现差不多。

 

 

更多相关文章

  1. Android 中使用代码动态网格布局
  2. Android四大组件之Content Provider
  3. Android 横向布局中间填满
  4. Android Jetpack系列之Lifecycle组件(一篇文章掌握Lifecycle)
  5. Android性能优化之布局优化
  6. Android中动态初始化布局参数以及ConstraintLayout使用中遇到的

随机推荐

  1. android系统中自带的图标大全
  2. 关于Android开发的editText控件怎…
  3. IDA - Support for Android
  4. Android交互式视觉设计控件二
  5. Android™ 1.6 android.R.drawable Icon
  6. 【Android】使用MediaCodec硬编码实现视
  7. android 圆角头像
  8. 380个Android实例
  9. Android安装卸载程序具体操作方法解析
  10. view属性大全