android 代码布局简单的例子

android 代码布局简单的例子
刚学android ,想搞明白代码布局是怎么弄的?附上一个简单的例子,写上几句注释,感激不尽。313219822@qq.com
(引用楼上的例子)
//初始化线性布局
LinearLayout linearLayout = new LinearLayout(context);
//设定线性布局的方向为竖直方向
linearLayout.setOrientation(LinearLayout.VERTICAL);
//设定线性布局的填充方式为自适应
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//设定线性布局内的对齐方式为控件水平居缓敬中
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
//初始化图片按钮

ImageButton news = new ImageButton(context);
//设定图片按钮的id
news.setId(85);
//为图片按钮设定监听器
news.setOnClickListener(this);
//为消哪粗图片按钮设定图片
news.setBackgroundResource(R.drawable.news);
//设定图片按钮的属性
news.setLayoutParams(new LayoutParams(33, 33));
//将图片按钮加入线性布局

linearLayout.addView(news);

简单的拿镇小例子就像上面这样了,其实很简单,就像 你要盖一间房子,里面有桌子板凳什么的,你就要设定桌子板凳的各种属性,长宽高颜色,样式什么的,然后 设定 桌子板凳放到 什么位置,最后 把桌子板凳按照你的想法 放在那个位置上就好了。
android LinearLayout 布局实例代码,参考如下:


xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>



android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">

android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>





Java代码 Views.java
package com.cn.view;

import android.app.Activity;
import android.os.Bundle;

public class Views extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

ImageButton news = new ImageButton(context);
news.setId(85);
news.setOnClickListener(this);
news.setBackgroundResource(R.drawable.news);
news.setLayoutParams(new LayoutParams(33, 33));

linearLayout.addView(news);

这段代码就动态枯橘袭把一个ImageButton添加伍散到一个LinearLayout中去了~

同时设定了VIEW的长宽背景布局方式等~

同理,删除掉使用的方法例如:

XXX.removeViewAt(index)

index就是这个子view在父view中是第(index+1)个添加进去的(index从0开始算的)

先尝没兄试去修改下,再有不懂的请留言~

帮你百度的 大致就这样 下面有参考资料http://zhidao.baidu.com/question/255392766.html
← 上一篇: 下一篇: →