已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 用纯代码实现复杂界面在开发Android应用时有时会遇到纯代码实现复杂界面的需求,本文通过实例来演示,希望能对大家有所帮助界面截图:XML布局文件: 通过纯代码实现XML同样的效果: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.);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_dialog_map);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);la
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高等学校物理学科试题及答案全解
- 乐高早年测验试题及答案解析
- 2025-2026学年忻州市岢岚县数学三年级下学期期中联考试题(含答案)
- 初中人教生物典型试题及答案呈现
- 成语练习题及精准解析答案
- 以考促学题目与答案
- 统计概率常见大题及标准答案解析
- 工业仓储考卷题目与答案详情
- 关于痔疮的多项选择题及答案解析
- 2026年起重指挥作业人员考试练习题【含答案】
- 2026年泌尿外科出科试卷及答案
- 2026小学数学北师大版新教材培训:四至六年级教材解析
- AI原生数据平台研究报告(2026年)(2026.6)
- 2026年成都玉林紫荆初一入学数学分班考试真题含答案
- 工程预应力张拉与灌浆质量控制措施
- 一氧化二氮的理化性质与危险特性培训
- 2026年江苏省安全员C2证(土建安全员)考试题库及答案
- DB11-T 1610-2026 民用建筑信息模型深化设计建模细度标准
- 2026年巨量本地推初级题库
- 保安员监控岗工作制度
- (正式版)DB36∕T 1297-2020 《城市消防物联网大数据应用平台物联设施设备接口规范》
评论
0/150
提交评论