在java代码中设置margin

我们平常可以直接在xml里设置margin,如:

<ImageView android:layout_margin="5dip" android:src="@drawable/image" />

但是有些情况下,需要在java代码里来写,可是View本身没有setMargin方法,怎么办呢?

通过查阅android api,我们发现android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).

其直接的子类有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.

使用方法:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);

原文:http://greatverve.cnblogs.com/archive/2012/01/29/android-margin.html

android 使用代码实现 RelativeLayout布局

 RelativeLayout rl = new RelativeLayout(this);

        Button btn1 = new Button(this);
        btn1.setText("----------------------");
        btn1.setId(1);

        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn1 位于父 View 的顶部,在父 View 中水平居中
        rl.addView(btn1, lp1 );

        Button btn2 = new Button(this);
        btn2.setText("|\n|\n|\n|\n|\n|");
        btn2.setId(2);

        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.addRule(RelativeLayout.BELOW, 1);
        lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
        // btn2 位于 btn1 的下方、其左边和 btn1 的左边对齐
        rl.addView(btn2, lp2);

        Button btn3 = new Button(this);
        btn3.setText("|\n|\n|\n|\n|\n|");
        btn3.setId(3);

        RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       lp3.addRule(RelativeLayout.BELOW, 1);
        lp3.addRule(RelativeLayout.RIGHT_OF, 2);
        lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
        // btn3 位于 btn1 的下方、btn2 的右方且其右边和 btn1 的右边对齐(要扩充)
        rl.addView(btn3,lp3);

        Button btn4 = new Button(this);
        btn4.setText("--------------------------------------------");
        btn4.setId(4);

        RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp4.addRule(RelativeLayout.BELOW, 2);
        lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn4 位于 btn2 的下方,在父 Veiw 中水平居中
        rl.addView(btn4,lp4);

        setContentView(rl);

原文:http://kukuqiu.iteye.com/blog/1018396

动态修改RelativeLayout的宽高:

参数可以.属性设置,但数值是像素,需要转化为dp单位。

RelativeLayout.LayoutParams linearParams =  (RelativeLayout.LayoutParams)rela_addnote_notetype.getLayoutParams();
        linearParams.height = 44;
        rela_addnote_notetype.setLayoutParams(linearParams);  

原文:http://blog.csdn.net/chenguang79/article/details/37874793

Android适配所需知识点LayoutParams:

http://www.android100.org/html/201406/05/18971.html

时间: 2024-08-04 15:56:39

在java代码中设置margin的相关文章

Android如何在java代码中设置margin

习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:src="@drawable/image" /> 只是有些情况下,需要在java代码里来写. API中,android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).可是View本身没

转--Android如何在java代码中设置margin

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有个

【安卓】在java代码中设置drawableLeft时如何给定合适尺寸?

textView.setCompoundDrawables(drawable, null, null, null);时看不到图片,是因为需要手动给定drawable对应的尺寸,即用drawable.setBounds. 如果该drawable为图片,可直接drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());. 即直接给定图片自身尺寸,此时效果和在xml中给定一样. [安卓]在java

在java代码中设置TextView的字体大小,单位设为sp

要实现的效果如标题. 在dimen.xml文件中设置了<dimen name="text_size">18sp</dimen> 开始的时候,通过sizeTv.setTextSize(getResource().getDimension(R.dimen.text_size))进行设置. 此时使用as的同学会发现,as提示的getDimension返回的值即为18sp.看着挺正常的. 但是跑出来的实际效果,会比在布局文件中直接设置18sp的字体来的大!! 暂时没去弄

android如何在代码中设置margin

1.首先要看下你要设置的这个组件的父容器是什么.比如我的是FrameLayout里面包含的一个组件.假如叫bottomLayout那么就是: FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.setMargins(0,100,0,0); bottomLayout.setLay

Java代码中获取Json的key值

测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym4iTZ3-ptWRRoVtbCNJnyjGLfed_4bB0HiqGP8v-aXOJUUk1tFs","expires_in":7200,"refresh_token":"_or_31yEp8XhxGWvrAzdWIkXfFlKjfb3iTRpfa

在Java代码上设置TextView字体大小

部分源码: /** * Set the default text size to a given unit and value. See {@link * TypedValue} for the possible dimension units. * * @param unit The desired dimension unit. * @param size The desired size in the given units. * * @attr ref android.R.styleab

java代码中获取进程process id(转)

另一方面,线程ID=进程ID+内部线程对象ID并不成立,    参考: blog.csdn.net/heyetina/article/details/6633901     如何在java代码中获取进程process id, 实现方法如下所示:    参考: rednaxelafx.iteye.com/blog/716918 http://www.cnblogs.com/mumuxinfei/p/3678854.html

如何在java代码中调用一个web项目jsp或者servlet

有时候需要调用一个web项目的jsp或者servlet,但是执行内部的代码,并不是打开jsp,例如需要在一段java代码中清除一个web项目中的缓存,那么可以把清除缓存的代码放在该web项目的一个servlet中,只需要执行如下代码: URL url = new URL("http://192.168.2.123:8080/sace/ClearCache"); url.openStream(); openStream() 执行一次相当于一次URL请求,其中url.openStream(