在android的布局中有四种布局,分别是:
1、LinearLayout 线性布局管理器,分为水平和垂直两种,只能进行单行布局。
2、FrameLayout 所有组件放在左上角, 一个覆盖一个。
3、TableLayout 任意行和列的表格布局管理器,其中TableRow代表一行,可以在行中增加组件。
4、RelativeLayout 相对布局管理器,根据最近的一个视图组件或顶级的父组件来确定下一个组件的位置。
那好吧,先从LinearLayout布局开始吧
先看效果:
第一张效果图是通过layout布局文件直接生成的
第二张图片是通过代码动态生成的
首先通过布局文件直接生成,这种比较简单,在layout中的xml文件中
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" > //设置布局管理器中的组件的排列方式是垂直的,当然还有水平的布局
- <Button
- android:id="@+id/but1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#00ffff" //设置Button的背景色
- android:textColor="#ffffff" //设置Button的字体颜色
- android:text="linearlayout1" //内容
- android:textSize="30dp"/> //字体大小
- <Button
- android:id="@+id/but2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#ff00ff"
- android:textColor="#ffffff"
- android:text="linearlayout2"
- android:textSize="30dp"/>
- <Button
- android:id="@+id/but3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#ffff00"
- android:textColor="#ffffff"
- android:text="linearlayout3"
- android:textSize="30dp" />
- <Button
- android:id="@+id/but4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#0000ff"
- android:textColor="#ffffff"
- android:text="linearlayout4"
- android:textSize="30dp"/>
- <Button
- android:id="@+id/but5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#00ff00"
- android:textColor="#ffffff"
- android:text="linearlayout5"
- android:textSize="30dp"/>
- <Button
- android:id="@+id/but6"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#ff0000"
- android:textColor="#ffffff"
- android:text="linearlayout6"
- android:textSize="30dp"/>
- <Button
- android:id="@+id/but7"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#333333"
- android:textColor="#ffffff"
- android:text="linearlayout7"
- android:textSize="30dp"/>
- <ImageView
- android:id="@+id/p_w_picpathview"
- android:layout_width="fill_parent"
- android:layout_height="90dp"
- android:src="@drawable/cocos2d" />
- </LinearLayout>
通过布局文件生成的只需要这个xml文件就可以了,是不是很简单,再看看通过代码动态的来生成的方式
- package com.example.linearlayoutproject2;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.view.Menu;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- public class MainActivity extends Activity {
- private Button redButton,greenButton,blueButton;
- private ImageView mImageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //定义一个布局管理器
- LinearLayout layout = new LinearLayout(MainActivity.this);
- //设置布局管理器的参数LinearLayout 是ViewGroup的子类
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.FILL_PARENT);
- //设置布局管理器中的组件的布局方式
- LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.WRAP_CONTENT);
- //设置布局管理器中的组件的布局方式是垂直方式
- layout.setOrientation(LinearLayout.VERTICAL);
- redButton = new Button(MainActivity.this);//构造一个Button
- redButton.setBackgroundColor(Color.RED); //设置背景色
- redButton.setText("LinearLayout1"); //设置内容
- redButton.setTextSize(30); //设置字体大小
- redButton.setTextColor(Color.WHITE); //设置字体颜色
- greenButton = new Button(MainActivity.this);
- greenButton.setBackgroundColor(Color.GREEN);
- greenButton.setText("LinearLayout2");
- greenButton.setTextSize(30);
- greenButton.setTextColor(Color.WHITE);
- blueButton = new Button(MainActivity.this);
- blueButton.setBackgroundColor(Color.BLUE);
- blueButton.setText("LinearLayout3");
- blueButton.setTextSize(30);
- blueButton.setTextColor(Color.WHITE);
- mImageView = new ImageView(MainActivity.this);//构造一个ImageView
- //从sdcard中图片一张图片
- mImageView.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/cocos2d.jpg"));
- //添加我们的组件
- layout.addView(redButton,buttonParams);
- layout.addView(greenButton,buttonParams);
- layout.addView(blueButton,buttonParams);
- layout.addView(mImageView,buttonParams);
- //最后设置我们的布局管理器
- super.setContentView(layout,params);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
通过代码实现也就这一个Activaty.java文件,两者的实现差不多。
转载于:https://blog.51cto.com/jackcheng/1131037