Android开发之LayoutParams的用法

来源:互联网 发布:ubuntu 精简 编辑:程序博客网 时间:2024/06/06 00:48

LayoutParams继承于Android.View.ViewGroup.LayoutParams.
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。

但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT,即填满(和父容器一样大小);
3,WRAP_CONTENT,即包裹住组件就好。
在JAVA中动态构建的布局,常常这样写:

?
1
setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
?
1
2
3
4
5
6
7
上面这一句话其实是子对父的,也就是说,父布局下的子控件要设置这句话。
 
 
因为布局很多,虽然都继承至ViewGroup但是各个布局还是有很大的不同。
 
 
很显然上面这句应该这样写才算准确:
?
1
setLayoutParams(newTableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));
?
1
2
3
4
5
6
这表示这个子控件的父布局是一个TableRow , 这样的LayoutParams 太多,所以应明确指明。
 
下面分别说下两个常用到布局:
1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现:这表示这个子控件的父布局是一个TableRow , 这样的LayoutParams 太多,所以应明确指明。
下面分别说下两个常用到布局:
1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现:
?
1
2
3
FrameLayout.LayoutParams lytp = newFrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp .gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);
?
1
2. RelativeLayout下动态设置子控件居中:
?
1
2
3
4
RelativeLayout.LayoutParams lp=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btn1.setLayoutParams(lp);

实例:

从网页响应获取Json 对象,并解析

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@SuppressWarnings(unchecked)
        Map<string,object> markJsonMap = (Map<string, object="">) mapJson.get(adlogo);
        if(markJsonMap != null){
            String position = (String) markJsonMap.get(position);
            String markTumbString = (String) markJsonMap.get(thumb);
            String smlscreen = (String) markJsonMap.get(smlscreen);
            String bigscreen = (String) markJsonMap.get(bigscreen);
            HomeActivity.mCornerMark.setPosition(position);
            HomeActivity.mCornerMark.setThumb(markTumbString);
            HomeActivity.mCornerMark.setSmlscreen(smlscreen);
            HomeActivity.mCornerMark.setBigscreen(bigscreen);
            setCornerMark();
        }</string,></string,object>
解析后代码设置角标图片的大小,位置,异步下载图片
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
     * 动态设置角标位置
     * */
    privatevoid setCornerMark(){
        if(!HomeActivity.mCornerMark.getThumb().equals() && HomeActivity.mCornerMark.getThumb().length() != 0)
        {
//          WindowManager wm = (WindowManager) mContex
//                  .getSystemService(Context.WINDOW_SERVICE);
//
//          int width = wm.getDefaultDisplay().getWidth();
//          int height = wm.getDefaultDisplay().getHeight();
 
            FrameLayout.LayoutParams reParams = (android.widget.FrameLayout.LayoutParams) HomeActivity.home_surface_viewLyout.getLayoutParams();
            intwidth = reParams.width;
            intheight = reParams.height;
            RelativeLayout.LayoutParams params;
            //width, height * 48 / 128
            //获取控件布局参数
            Configuration config = mContex.getResources().getConfiguration();
            if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                String[] paramString = HomeActivity.mCornerMark.getBigscreen().split(,);
                intwhidthString = Integer.parseInt(paramString[0]);
                intheightString = Integer.parseInt(paramString[1]);
                //转dp
                whidthString = DensityUtil.px2dip(mContex,whidthString);
                heightString = DensityUtil.px2dip(mContex,heightString);
                params = newLayoutParams(whidthString,heightString);
            }else{
                String[] paramString = HomeActivity.mCornerMark.getSmlscreen().split(,);
                intwhidthString = Integer.parseInt(paramString[0]);
                intheightString = Integer.parseInt(paramString[1]);
                //转dp
                whidthString = (int) (width*0.005*whidthString);
                heightString = (int) (height*0.005*heightString);
                 
                whidthString = DensityUtil.px2dip(mContex,whidthString);
                heightString = DensityUtil.px2dip(mContex,heightString);
                params = newRelativeLayout.LayoutParams(whidthString,heightString);
            }
 
            //动态指定控件大小,位置
            Log.v(Position, HomeActivity.mCornerMark.getPosition());
            if(HomeActivity.mCornerMark.getPosition().equals(1)){
                params.leftMargin=20;
                params.topMargin=20;
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            }
            elseif(HomeActivity.mCornerMark.getPosition().equals(2)){
                params.rightMargin=20;
                params.topMargin=20;
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            }elseif (HomeActivity.mCornerMark.getPosition().equals(3)) {
                params.leftMargin=20;
                params.bottomMargin=20;
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            }elseif(HomeActivity.mCornerMark.getPosition().equals(4)){
                params.rightMargin=20;
                params.bottomMargin=20;
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            }
 
 
            HomeActivity.img_cornermark.setLayoutParams(params);
 
            DownLoadTask downLoadTask = newDownLoadTask(HomeActivity.img_cornermark);
            downLoadTask.execute(HomeActivity.mCornerMark.getThumb());
 
        }
    }
遇到的问题:

1. 在RelativeLayout里的布局,图片不显示?

RelativeLayout 层次布局是通过xml 文件 Relativelayout 由底层到外层进行布局的 ,应该在底层布局之上 
2.在RelativeLayout里,代码实现位置是用
?
1
2
<span style="white-space:pre">  </span>params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

3.不同手机分辨率等比列显示:

?
1
2
3
4
5
6
7
8
9
10
String[] paramString = HomeActivity.mCornerMark.getSmlscreen().split(,);
                intwhidthString = Integer.parseInt(paramString[0]);
                intheightString = Integer.parseInt(paramString[1]);
                //转dp
                whidthString = (int) (width*0.005*whidthString);
                heightString = (int) (height*0.005*heightString);
                 
                whidthString = DensityUtil.px2dip(mContex,whidthString);
                heightString = DensityUtil.px2dip(mContex,heightString);
                params = newRelativeLayout.LayoutParams(whidthString,heightString);

 


0 0
原创粉丝点击