Android UI详解之动态布局

 

1.相对布局RelativeLayout

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//定义一个相对布局RelativeLayout rl = new RelativeLayout(this);Button bt = new Button(this);bt.setText("MYbutton");bt.setId(1);//添加子元素//rl.addView(bt);                  //定义参数,这个制定样式,也就是布局空间的位置RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);Params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);//btn1位于父View的顶部,在父View中水平居中rl.addView(bt,Params);setContentView(rl);}


2、定义一个线性布局LinearLayout

           一个LinearLayout 和 这个LinearLayout里边一个 TextView 的关系 TextView 就算LinearLayout的子视图 child view .需要注意的是LayoutParams只是ViewGroup的一个内部类 这里边这个也就是ViewGroup里边这个LayoutParams类是 base class 基类 实际上每个不同的ViewGroup都有自己的LayoutParams子类   比如LinearLayout 也有自己的 LayoutParams 大家打开源码看几眼就知道了

//创建一个线性布局       private LinearLayout mLayout;      mLayout = (LinearLayout) findViewById(R.id.layout);      //现在我要往mLayout里边添加一个TextView       //你可能会想直接在布局文件里边配置不就行了 那是 但是这里为了说明问题我们用代码实现       TextView textView = new TextView(Activity01.this);      textView.setText("Text View" );      //这里请不要困惑,这里是设置 这个textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里边设置是一样的如       /***/  //在xml里边怎么配置高宽大家都会的。       //第一个参数为宽的设置,第二个参数为高的设置。       LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(      LinearLayout.LayoutParams.FILL_PARENT,      LinearLayout.LayoutParams.WRAP_CONTENT      1);      //调用addView()方法增加一个TextView到线性布局中       mLayout.addView(textView, p);


上面代码就是先加载已经存在的Layout文件,然后再用代码动态的改变,这种很常见。

 

LayoutParams继承于Android.View.ViewGroup.LayoutParams.
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。

 


但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT,即填满(和父容器一样大小);
3,WRAP_CONTENT,即包裹住组件就好

 

Demo

Android UI详解之动态布局_第1张图片

XML代码

<?xml version="1.0" encoding="utf-8"?>                                 



等效的java代码

 

import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.text.InputFilter;import android.text.InputFilter.LengthFilter;import android.view.Gravity;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.ScrollView;import android.widget.TextView;public class ActivityInfo extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//setContentView(R.layout.info);initUI();}public final void initUI(){ScrollView main = new ScrollView(this);main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));main.setBackgroundColor(Color.WHITE);//根布局参数LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);layoutParamsRoot.gravity = Gravity.CENTER;//根布局LinearLayout layoutRoot = new LinearLayout(this);layoutRoot.setLayoutParams(layoutParamsRoot);layoutRoot.setOrientation(LinearLayout.VERTICAL);//上边距(dp值)int topMargin = dip2px(this, 30);//imageMain宽度(dp值)int widthMain = dip2px(this, 240);//imageMain高度(dp值)int heightMain = dip2px(this, 120);//imageMain布局参数LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(widthMain,heightMain);layoutParamsImageMain.topMargin = topMargin;layoutParamsImageMain.bottomMargin = topMargin;layoutParamsImageMain.leftMargin = topMargin;layoutParamsImageMain.rightMargin = topMargin;layoutParamsImageMain.gravity=Gravity.CENTER_HORIZONTAL;//初始化ImageViewImageView imageMain = new ImageView(this);imageMain.setScaleType(ScaleType.FIT_CENTER);imageMain.setAdjustViewBounds(true);imageMain.setBackgroundColor(Color.BLACK);imageMain.setImageResource(android.R.drawable.ic_launcher);layoutRoot.addView(imageMain, layoutParamsImageMain);//textInfo布局参数LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);layoutParamsTextInfo.topMargin = topMargin;layoutParamsTextInfo.bottomMargin = topMargin;layoutParamsTextInfo.leftMargin = topMargin;layoutParamsTextInfo.rightMargin = topMargin;layoutParamsTextInfo.gravity=Gravity.CENTER_HORIZONTAL;//初始化textInfoTextView textInfo = new TextView(this);textInfo.setGravity(Gravity.CENTER_HORIZONTAL);textInfo.setTextSize(18);layoutRoot.addView(textInfo, layoutParamsTextInfo);//editInfo布局参数LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(widthMain,LayoutParams.WRAP_CONTENT);layoutParamsEditInfo.topMargin = topMargin;layoutParamsEditInfo.gravity=Gravity.CENTER_HORIZONTAL;//初始化editInfoEditText editInfo = new EditText(this);editInfo.setHint("请输入文字内容");//设置可输入的最大长度InputFilter[] filters = {new LengthFilter(200)};  editInfo.setFilters(filters);editInfo.setTextSize(18);layoutRoot.addView(editInfo, layoutParamsEditInfo);//上边距(dp值)int minHeight = dip2px(this, 54);//上padding(dp值)int topPadding = dip2px(this, 4);//左padding(dp值)int leftPadding = dip2px(this, 2);//按钮布局LinearLayout layoutButton = new LinearLayout(this);layoutButton.setLayoutParams(layoutParamsEditInfo);layoutButton.setOrientation(LinearLayout.HORIZONTAL);layoutButton.setBackgroundColor(Color.parseColor("#c6c3c6"));layoutButton.setMinimumHeight(minHeight);layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding);layoutButton.setId(100000001);//buttonOK布局参数LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);layoutParamsButtonOK.gravity = Gravity.LEFT;layoutParamsButtonOK.leftMargin = dip2px(this, 10);layoutParamsButtonOK.rightMargin = dip2px(this, 5);layoutParamsButtonOK.weight = 1;//Button确定Button buttonOK = new Button(this);buttonOK.setLayoutParams(layoutParamsButtonOK);buttonOK.setMaxLines(2);buttonOK.setTextSize(18);buttonOK.setText("确定");layoutButton.addView(buttonOK);//buttonCancel布局参数LinearLayout.LayoutParams layoutParamsButtonCancel = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);layoutParamsButtonCancel.gravity = Gravity.RIGHT;layoutParamsButtonCancel.leftMargin = dip2px(this, 5);layoutParamsButtonCancel.rightMargin = dip2px(this, 10);layoutParamsButtonCancel.weight = 1;//Button取消Button buttonCancel = new Button(this);buttonCancel.setLayoutParams(layoutParamsButtonCancel);buttonCancel.setMaxLines(2);buttonCancel.setTextSize(18);buttonCancel.setText("取消");layoutButton.addView(buttonCancel);layoutRoot.addView(layoutButton, layoutParamsEditInfo);//RelativeLayout布局参数LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);RelativeLayout layoutBottom = new RelativeLayout(this);layoutBottom.setLayoutParams(layoutParamsBottom);RelativeLayout.LayoutParams paramsImageBottom = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);paramsImageBottom.addRule(RelativeLayout.BELOW, 100000001);paramsImageBottom.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);paramsImageBottom.setMargins(topMargin, topMargin, topMargin, topMargin);//初始化ImageViewImageView imageBottom = new ImageView(this);imageBottom.setScaleType(ScaleType.FIT_CENTER);imageBottom.setAdjustViewBounds(true);imageBottom.setBackgroundColor(0xFF777777);imageBottom.setImageResource(android.R.drawable.ic_dialog_email);layoutBottom.addView(imageBottom, paramsImageBottom);layoutRoot.addView(layoutBottom);//TODO TEST//imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map);textInfo.setText("测试文本显示");main.addView(layoutRoot);setContentView(main);}/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}/** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */public static int px2dip(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f);}}


 

更多相关文章

  1. Android 常用布局
  2. 时钟控件布局
  3. Android软键盘手动显示、隐藏、布局上移和EditText上移
  4. Andorid在布局文件中中文加粗
  5. Android图文布局【整理】
  6. android 自学笔记2-布局
  7. Android RecyclerView多个Item布局的实现(可实现头部底部)
  8. Android计算器界面布局
  9. android 不使用布局文件,完全由代码控制布局实例

随机推荐

  1. 【android ndk】macos环境下Android(安卓
  2. Android的Handler总结
  3. Android上的手势监听实现
  4. Android之AsyncTask
  5. 基于AOA协议实现Android设备的USB通信
  6. 关于Android架构组件,Android(安卓)Archit
  7. Android亮度调节的几种实现方法
  8. android中文乱码问题(亲测有效)
  9. android 使用SharedPreferences对数据存
  10. Android的进程线程