Android 用纯代码实现复杂界面

本文详细介绍如何使用纯代码在Android应用中构建复杂的用户界面,包括ScrollView、LinearLayout、ImageView、TextView、EditText和Button等组件的布局及属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

在开发Android应用时有时会遇到纯代码实现复杂界面的需求,本文通过实例来演示,希望能对大家有所帮助

界面截图

 

XML布局文件:

<?xml version="1.0" encoding="utf-8"?><ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/white">        <LinearLayout      android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:gravity="center">     <ImageView          android:layout_width="240dip"         android:layout_height="120dip"         android:layout_margin="30dip"         android:layout_gravity="center_horizontal"         android:background="@android:color/black"         android:scaleType="fitCenter"         android:adjustViewBounds="true"         android:src="@android:drawable/ic_dialog_map"/>     <TextView         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_margin="30dip"         android:layout_gravity="center_horizontal"         android:gravity="center_horizontal"         android:textSize="18sp"         android:text="测试文本显示"/>     <EditText          android:layout_width="240dip"         android:layout_height="wrap_content"         android:layout_margin="30dip"         android:layout_gravity="center_horizontal"         android:hint="请输入文字内容"         android:maxLength="200"         android:textSize="18sp"/>     <LinearLayout          android:id="@+id/button_layout"         android:layout_width="240dip"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:background="#c6c3c6"         android:minHeight="54dip"         android:orientation="horizontal"         android:paddingTop="4dip"         android:paddingBottom="4dip"         android:paddingLeft="2dip"         android:paddingRight="2dip" >         <Button               android:text="确定 "             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="left"             android:layout_marginLeft="10dip"             android:layout_marginRight="5dip"             android:layout_weight="1"             android:maxLines="2"             android:textSize="18sp" />         <Button              android:text="取消"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="right"             android:layout_marginLeft="5dip"             android:layout_marginRight="10dip"             android:layout_weight="1"             android:maxLines="2"             android:textSize="18sp"/>     </LinearLayout>     <RelativeLayout          android:layout_width="fill_parent"         android:layout_height="wrap_content"         >         <ImageView              android:id="@+id/ImageBottom"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@id/button_layout"          android:layout_centerHorizontal="true"          android:layout_margin="30dip"          android:background="#FF777777"          android:scaleType="fitCenter"          android:adjustViewBounds="true"          android:src="@android:drawable/ic_dialog_email"/>     </RelativeLayout>      </LinearLayout>    </ScrollView>


通过纯代码实现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 {  @Override protected void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.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;  //初始化ImageView  ImageView 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;  //初始化textInfo  TextView 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;  //初始化editInfo  EditText 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);    //初始化ImageView  ImageView 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); }}


 

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

VC6界面类编程小实例\A Drag and Drop List Control.doc ...................\MFC窗口位置管理详细分析及实例.doc ...................\Toolbar制作菜单条过程详解.doc ...................\VC++6.0定制窗口的方法.doc ...................\VC++中MFC窗口对象的清除.doc ...................\VC++任务栏提示区图标的实现.doc ...................\VC++动态加入和删除菜单的方法.doc ...................\VC++环境下浮动工具条的编程.doc ...................\VC++环境下菜单和工具栏的动态修改技术.doc ...................\VC中如何捕获和释放鼠标.doc ...................\VC控件 TreeCtrl 与 ListCtrl 演示.doc ...................\VC界面编程总结.doc ...................\VC编程中如何在界面实现3D文字.doc ...................\VC编程实现IE风格的界面.doc ...................\VC编程实现IE风格的界面之叙述.doc ...................\VC设计分割视图通用创建框架.doc ...................\VC通用控件编程之CImageList控件.doc ...................\Visual C++中位图按钮的新颖设计.doc ...................\Visual C++中实现对图像数据的读取显示.doc ...................\Visual C++中的图形特技.doc ...................\Visual C++模态对话框消息处理机制的分析.doc ...................\Visual C++窗体设计技巧集.doc ...................\Visual C++窗口标题改变的几种方法.doc ...................\Windows 界面设计:拉帘按钮设计.doc ...................\XListBox-自画条目背景和文字颜色的listbox控件.doc ...................\XP风格控件界面库.doc ...................\一个功能强大的MFC界面处理扩展库:CJ60Lib.doc ...................\一个屏幕捕捉的例子.doc ...................\一段使窗口透明的代码(仅适用于2000).doc ...................\一种漂亮的自绘菜单.doc ...................\为CListBox加上智能水平滚动条.doc ...................\为列表控件添加水平滚动条.doc ...................\为对话框程序添加工具条和状态栏.doc ...................\从资源中加载皮肤, 使用免费界面库 AppFace For VC 0.2 美化您的软件.doc ...................\位图文件读写综述.doc ...................\使用CRectTracker类进行对象动态定位.doc ...................\使用CTabCtrl控件实现属性页功能.doc ...................\使用测试优先方法开发用户界面.doc ...................\关于内存DC绘图防止屏幕闪烁说明.doc ...................\创建有个性的对话框之MFC篇.doc ...................\利用VC编程在界面实现3D文字.doc ...................\利用钩子实现菜单阴影效果.doc ...................\制作 MSN、QQ 的消息提示窗口.doc ...................\单文档与多视的实现方法.doc ...................\图像平滑滚动效果的VC实现.doc ...................\在(CListView)列表视图中添加右键菜单.doc ...................\在ATL Browser Helper对象中使用WTL TreeView.doc ...................\在SDI MDI程序的工具条上加入漂亮的标题头.doc ...................\在VC++6.0开发中实现全屏显示.doc ...................\在VC下显示JPEG、GIF格式图像的一种简便方法.doc ...................\在VC中用CMenuXP使应用程序拥有Office XP风格的界面.doc ...................\在VC中透明浮动按键的实现.doc ...................\在Windows95,98中实现苹果窗口界面.doc ...................\在单文档中添加系统菜单项.doc ...................\在单文档程序中动态切换多个窗体.doc ...................\在对话框上加超链接.doc ...................\在类VC的界面实现中加入目录树.doc ...................\在视图中同步显示鼠标的位置.doc ...................\如何创建一个不规则形状的窗口.doc ...................\如何创建无模式对话框.doc ...................\如何创建有模式对话框.doc ...................\如何制作弹出式菜单.doc ...................\如何在VC中改变控件的背景色.doc ...................\如何在状态条中加入图像.doc ...................\如何模拟《WORD》的窗口形式.doc ...................\如何编写类似于Word97的工具栏.doc ...................\如何隐藏显示在任务栏中的对话框程序.doc ...................\实现带阴影弹出的窗口.doc ...................\实现真正的Windows屏幕保护程序.doc ...................\对CXPStyleButtonST的改造.doc ...................\对话框隐藏的几种方法.doc ...................\带有菜单的EDIT控件实现.doc ...................\平面、带图片的按钮.doc ...................\扩展COleDropTarget类来支持任意窗口拖放.doc ...................\拖拉机客户端任务书.doc ...................\数据库开发之窗体编程.doc ...................\文档与视图的建立和关联.doc ...................\无闪烁刷屏技术的实现.doc ...................\显示作为窗口或对话框背景的位图.doc ...................\最简单的界面增强库EasySkin.doc ...................\树视控件在多文档中的使用.doc ...................\根据文本串的长度改变窗口的大小.doc ...................\用MFC对话框做无闪烁图片重绘.doc ...................\用SDK实现分隔条.doc ...................\用VC++制作有滚动字幕效果的软件封面.doc ...................\用VC设计托盘图标程序.doc ...................\用Visual C++实现带阴影弹出窗口的技术.doc ...................\用户界面设计的技巧与技术.doc ...................\百叶窗式面板组.doc ...................\窗口类的诞生.doc ...................\类似Vc和Outlook的界面.doc ...................\编程打开特殊的系统窗口.doc ...................\自绘菜单的实现.doc ...................\自绘边框窗口.doc ...................\让对话框显示背景图.doc ...................\谈VC++中的Progress控件的使用.doc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值