Android 代码写控件代替XML简单实例
作者:lqh 时间:2023-04-11 00:10:48
Android 代码写控件代替XML简单实例
简单的一个Button控件的练习。
实现代码:
Button btn = new Button(HandlerToActivity.this);
LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
params.topMargin = 10;
btn.setLayoutParams(params);
btn.setLayoutParams(params);
btn.setTextColor(Color.WHITE);
btn.setPadding(0, 50, 0, 2);
btn.setGravity(Gravity.CENTER_HORIZONTAL);
btn.setTextSize(14);
btn.setText("这是Button");
btn.setBackgroundResource(R.drawable.alertdialog_button);
buttonLayout.addView(btn);
需要注意的问题:
1,定义LayoutParams,有些属性直接通过Button控件设置,有些是在Params中,比如gravity,margin等。
2,还要定义一个Layout布局接收。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/mo_feng_/article/details/52597048
标签:Android,控件,XML
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android-App增量更新的使用姿势
2022-09-22 22:47:38
![](https://img.aspxhome.com/file/2023/7/139137_0s.jpg)
C#实现改变DataGrid某一行和单元格颜色的方法
2022-08-23 20:45:22
WinForm调用jar包的方法分析
2023-11-17 04:09:46
![](https://img.aspxhome.com/file/2023/9/94589_0s.png)
浅析Android 快速实现图片压缩与上传功能
2022-10-15 23:58:01
深入android Unable to resolve target 'android-XX'详解
2023-05-29 13:21:33
一文详解如何在Flutter中使用导航Navigator
2022-06-15 05:52:32
![](https://img.aspxhome.com/file/2023/0/137680_0s.jpg)
Android实现图片在屏幕内缩放和移动效果
2021-10-28 12:24:57
![](https://img.aspxhome.com/file/2023/9/96809_0s.gif)
java图片识别文字的方法
2021-11-11 02:43:03
idea中的Maven导包失败问题解决方案汇总
2023-07-12 12:34:22
![](https://img.aspxhome.com/file/2023/7/70567_0s.png)
Android XListView下拉刷新和上拉加载更多
2022-11-01 19:07:45
![](https://img.aspxhome.com/file/2023/8/126698_0s.gif)
Spring Boot集成ElasticSearch实现搜索引擎的示例
2021-06-02 05:06:16
![](https://img.aspxhome.com/file/2023/3/63793_0s.png)
HashMap红黑树入门(实现一个简单的红黑树)
2022-10-18 13:52:35
![](https://img.aspxhome.com/file/2023/2/79292_0s.png)
简单了解Thymeleaf语法 数据延迟加载使用实例
2021-11-27 04:05:49
Spring Boot中配置定时任务、线程池与多线程池执行的方法
2021-08-08 13:31:30
浅谈java中定义泛型类和定义泛型方法的写法
2023-04-19 14:28:59
Android图片添加水印图片并把图片保存到文件存储的实现代码
2022-06-03 21:48:54
springboot-2.3.x最新版源码阅读环境搭建(基于gradle构建)
2022-11-19 01:47:41
![](https://img.aspxhome.com/file/2023/9/121929_0s.jpg)
android中LinearLayoutManager一键返回顶部示例
2022-02-23 19:19:26
![](https://img.aspxhome.com/file/2023/7/139517_0s.gif)
spring mvc高级技术实例详解
2022-10-11 12:50:15
![](https://img.aspxhome.com/file/2023/1/75511_0s.png)
详解Java Map中三个冷门容器的使用
2021-09-02 14:27:22
![](https://img.aspxhome.com/file/2023/3/119073_0s.png)