转--Android如何在java代码中设置margin
======== 3
在Java代码里设置button的margin(外边距)?
1、获取按钮的LayoutParams
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)button.getLayoutParams();
2、在LayoutParams中设置margin
layoutParams.setMargins(100,20,10,5);//4个参数按顺序分别是左上右下
3、把这个LayoutParams设置给按钮
button.setLayoutParams(layoutParams);
=========== 2
怎样用java代码生成按钮并设置它的位置啊?
给你写个最简单的吧,其实和XML布局是一样的。都是在LinearLayout、RelativeLayout这些布局中加控件,不同的是java代码都是new 出来的对象。
例子: LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(0, 0, 0, 0);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); Button btn = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0); btn.setLayoutParams(params);
layout.addView(btn);
============= 1
http://www.2cto.com/kf/201207/140111.html
红黑联盟:
习惯了直接在xml里设置margin(距离上下左右都是10dip),如:
<ImageView android:layout_margin="10dip" android:src="@drawable/image" />
只是有些情况下,需要在java代码里来写。
API中,android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom)。
可是View本身没有setMargin方法,怎么办呢?
看见API上,其直接的子类有:
FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams。
我们我们可以这样写:
ImageView imageView = = new ImageView(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); // , 1是可选写的
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);
==================
转--Android如何在java代码中设置margin的更多相关文章
- Android如何在java代码中设置margin
习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:s ...
- Android 如何在Java代码中手动设置控件的marginleft
1.定义LayoutParams LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.La ...
- Android在java代码中设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- 在java代码中设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- android中在java代码中设置Button按钮的背景颜色
android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...
- 详解Spring中的CharacterEncodingFilter--forceEncoding为true在java代码中设置失效--html设置编码无效
在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...
- 如何在java代码中调用一个web项目jsp或者servlet
有时候需要调用一个web项目的jsp或者servlet,但是执行内部的代码,并不是打开jsp,例如需要在一段java代码中清除一个web项目中的缓存,那么可以把清除缓存的代码放在该web项目的一个se ...
- android 可以在程序代码中设置样式:style
<style name="text_style"> <item name="android:textStyle">bold</it ...
- 如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量
本文使用的例子源代码在我的github上. 在我的公众号文章在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务介绍了如何通过Cloud Connector ...
随机推荐
- (转) SLAM系统的研究点介绍 与 Kinect视觉SLAM技术介绍
首页 视界智尚 算法技术 每日技术 来打我呀 注册 SLAM系统的研究点介绍 本文主要谈谈SLAM中的各个研究点,为研究生们(应该是博客的多数读者吧)作一个提纲挈领的摘要.然后,我 ...
- Performance Analysis Methodology
http://www.brendangregg.com/methodology.html The USE Method: for finding resource bottlenecks The TS ...
- tomcat maven book
https://www.amazon.com/gp/aw/d/0596517335/ref=pd_aw_sim_14_3?ie=UTF8&psc=1&refRID=H34H0BX247 ...
- mysql学习之-show table status(获取表的信息)参数说明
--获取表的信息mysql> show table status like 'columns_priv'\G;*************************** 1. row ******* ...
- MySQL 日志管理详解
大纲 一.日志分类 二.日志详解 注:MySQL版本,Mysql-5.5.32(不同版本的mysql变量有所不同) 一.日志分类 错误日志 查询日志 慢查询日志 二进制日志 中继日志 事务日志 滚动日 ...
- Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> 转载
Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> mvcmvc:resources ...
- 00-Java 语言简介
一.开发环境搭建: (一).JAVA语言简介: 1.JAVA语言简介: (1)什么是JAVA:Java是一种计算机编程语言.它是一种计算机编程语言.它是一种软件开发平台.它是一种软件运行平台.它是一种 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- no ocijdbc11 in java.library.path linux
no ocijdbc11 in java.library.path linux vi /etc/profile export ORACLE_HOME=/oracle/database/oracle/p ...
- Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法
Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法 最近在适用Ext JS4开发 ...