Android动态添加设置布局与控件的方法

时间:2022-02-27 09:19:50

本文实例讲述了android动态添加设置布局与控件的方法。分享给大家供大家参考,具体如下:

有时候我们会在代码端,动态的设置,添加布局和控件。下面我们就看来看一下如何处理,直接上代码,代码里面的注解很清楚了。

布局文件:fragment_hot.xml

说明:这个部局,我用的是scrollview做为基础布局,主要是为了实现一个滚动。这里不多说,这个你可以使用任何布局都可以,这里的id我是提前定义的。

这里面的现在有的布局是我为了看到我在代码端,动态添加的代码,是否可以追加到现有布局的后面而加上,这里大家可以自己设置

?
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?xml version="1.0" encoding="utf-8"?>
<scrollview xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@id/id_stickynavlayout_innerscrollview"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <linearlayout
    android:id="@+id/line_fragment_hot_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <linearlayout
      android:layout_width="fill_parent"
      android:layout_height="80dp"
      android:orientation="horizontal">
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1">
        <imageview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/desktop_device"/>
      </linearlayout>
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊孔雀城"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊 燕郊"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="3万抵6万"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="6000元/m"/>
      </linearlayout>
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:gravity="right">
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="剩2天"
          android:textcolor="#f97f49"
          android:textsize="12dp"
          android:textstyle="bold"
          android:layout_margintop="2dp"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/rectangle_radius_fen"
          android:gravity="center"
          android:text="团"
          android:textsize="9dp"
          android:textcolor="#ffffff"
          android:layout_marginright="5dp"
          android:layout_marginleft="2dp"/>
      </linearlayout>
    </linearlayout>
    <linearlayout
      android:layout_width="fill_parent"
      android:layout_height="80dp"
      android:orientation="horizontal">
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1">
        <imageview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/desktop_device"/>
      </linearlayout>
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊孔雀城"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊 燕郊"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="3万抵6万"/>
        <textview
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="6000元/m"/>
      </linearlayout>
      <linearlayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:gravity="right">
        <textview
          android:layout_width="wrap_content"
          android:layout_height="20dp"
          android:text="剩2天"/>
        <textview
          android:layout_width="10dp"
          android:layout_height="10dp"
          android:background="@drawable/rectangle_radius_fen"
          android:gravity="center"
          android:text="团"
          android:textsize="5dp"
          android:layout_marginright="5dp"/>
      </linearlayout>
    </linearlayout>
  </linearlayout>
</scrollview>

一个背景图文件在drawable文件夹中

rectangle_radius_fen.xml

说明:很简单,就是设置一个有圆角的方形

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <!-- 定义填充渐变颜色 -->
  <solid android:color="#f97f49"/>
  <!-- 设置内填充 -->
  <padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"/>
  <!-- 设置圆角矩形 -->
  <corners android:radius="2dp"/>
</shape>

后台代码:

fragment_hot.java

说明:这里不多说了,里面的注解很明确了

?
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.cg.fangduo;
import android.content.context;
import android.graphics.color;
import android.os.bundle;
import android.support.v4.app.fragment;
import android.util.typedvalue;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.scrollview;
import android.widget.textview;
/**
 * a simple {@link fragment} subclass.
 */
public class fragment_hot extends fragment {
  //定义当前页面
  private view view;
  private linearlayout line_fragment_hot_main;
  private scrollview id_stickynavlayout_innerscrollview;
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container,
               bundle savedinstancestate) {
    view = inflater.inflate(r.layout.fragment_hot, container, false);
    id_stickynavlayout_innerscrollview = (scrollview)view.findviewbyid(r.id.id_stickynavlayout_innerscrollview);
    line_fragment_hot_main = (linearlayout)view.findviewbyid(r.id.line_fragment_hot_main);
    for(int i=0;i<10;i++) {
      /**
       * 设置每个item最外层的linearlayout
       */
      linearlayout itemmain = new linearlayout(getactivity());
      itemmain.setorientation(linearlayout.horizontal); //设置linearlayout是横向还是竖各
      linearlayout.layoutparams itemmainparams = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, dip2px(getactivity(), 80)); //设置宽与高
      itemmainparams.setmargins(0,dip2px(getactivity(),1),0,0); //设置每个item与上一个控件的间隔是1dip
      itemmain.setlayoutparams(itemmainparams);
      itemmain.setbackgroundcolor(color.white); //设置背景色
      itemmain.setverticalgravity(gravity.center); //设置对齐方式
      /**
       * 设置左侧图片linearlayout
       */
      linearlayout itempic = new linearlayout(getactivity());
      itempic.setorientation(linearlayout.horizontal);
      linearlayout.layoutparams itempicparams = new linearlayout.layoutparams(0, dip2px(getactivity(), 70), 1);
      itempic.setlayoutparams(itempicparams);
      itempic.setverticalgravity(gravity.center_vertical);
      /**
       * 设置图片
       */
      imageview imgpic = new imageview(getactivity());
      imgpic.setimageresource(r.drawable.desktop_device);
      linearlayout.layoutparams imgpicparams = new linearlayout.layoutparams(dip2px(getactivity(), viewgroup.layoutparams.wrap_content), dip2px(getactivity(), viewgroup.layoutparams.wrap_content));
      imgpicparams.leftmargin = dip2px(getactivity(), 5);
      imgpicparams.gravity = gravity.left;  //必须要加上这句,setmargins才会起作用,而且此句还必须在setmargins下面
      imgpic.setlayoutparams(imgpicparams);
      /**
       * 图片linearlayout加载图片
       */
      itempic.addview(imgpic, imgpicparams);
      /**
       * 向主linearlayout加载图片linearlayout
       */
      itemmain.addview(itempic);
      /**
       * 设置中间文字显示linearlayout
       */
      linearlayout itemtext = new linearlayout(getactivity());
      itemtext.setorientation(linearlayout.vertical);
      linearlayout.layoutparams itemtextparams = new linearlayout.layoutparams(0, dip2px(getactivity(), 70), 1);
      itemtext.setlayoutparams(itemtextparams);
      itemtext.setverticalgravity(gravity.center_vertical);
      textview txtone = new textview(getactivity());
      txtone.settext("珠江俊景小区");
      txtone.settextsize(typedvalue.complex_unit_dip, 12); //设置字号,并且设置它的单位为dip
      txtone.getpaint().setfakeboldtext(true); //字体加租
      linearlayout.layoutparams txtoneparams = new linearlayout.layoutparams(dip2px(getactivity(), viewgroup.layoutparams.wrap_content), dip2px(getactivity(), viewgroup.layoutparams.wrap_content));
      txtoneparams.weight = 1;
      txtoneparams.setmargins(0, 0, 0, 0);
      txtoneparams.gravity = gravity.left;
      txtone.setlayoutparams(txtoneparams);
      itemtext.addview(txtone);
      textview txttwo = new textview(getactivity());
      txttwo.settext("哈尔滨");
      txttwo.getpaint().setfakeboldtext(true);
      txttwo.settextsize(typedvalue.complex_unit_dip, 8);
      linearlayout.layoutparams txttwoparams = new linearlayout.layoutparams(dip2px(getactivity(), viewgroup.layoutparams.wrap_content), dip2px(getactivity(), viewgroup.layoutparams.wrap_content));
      txttwoparams.weight = 1;
      txttwo.setlayoutparams(txttwoparams);
      itemtext.addview(txttwo);
      textview txtthree = new textview(getactivity());
      txtthree.settext("一万抵五万");
      txtthree.getpaint().setfakeboldtext(true);
      txtthree.settextsize(typedvalue.complex_unit_dip, 8);
      txtthree.setlayoutparams(txtoneparams);
      itemtext.addview(txtthree);
      textview txtfour = new textview(getactivity());
      txtfour.settext("8000元/m");
      txtfour.settextsize(typedvalue.complex_unit_dip, 10);
      txtfour.settextcolor(color.parsecolor("#f97f49"));
      txtfour.getpaint().setfakeboldtext(true);
      txtfour.setlayoutparams(txtoneparams);
      itemtext.addview(txtfour);
      itemmain.addview(itemtext);
      /**
       * 定义右侧显示信息框架
       */
      linearlayout line_right_text = new linearlayout(getactivity());
      line_right_text.setorientation(linearlayout.horizontal);
      linearlayout.layoutparams rightparams = new linearlayout.layoutparams(0, dip2px(getactivity(), 70), 1);
      line_right_text.setlayoutparams(rightparams);
      line_right_text.setgravity(gravity.right);  //右对齐
      textview daytxt = new textview(getactivity());
      linearlayout.layoutparams daytxtparams = new linearlayout.layoutparams(dip2px(getactivity(), viewgroup.layoutparams.wrap_content), dip2px(getactivity(), viewgroup.layoutparams.wrap_content));
      daytxt.setlayoutparams(daytxtparams);
      daytxt.settext("剩2天");
      daytxt.settextcolor(color.parsecolor("#f97f49")); //设置颜色
      daytxt.settextsize(typedvalue.complex_unit_dip, 10); //设置文字大小
      daytxt.getpaint().setfakeboldtext(true); //设置文字样式,加粗
      //daytxt.setpadding(0,2,2,0);
      line_right_text.addview(daytxt);
      textview tuantxt = new textview(getactivity());
      linearlayout.layoutparams tuantxtparams = new linearlayout.layoutparams(dip2px(getactivity(), 12), dip2px(getactivity(), 12));
      tuantxtparams.setmargins(dip2px(getactivity(), 2), dip2px(getactivity(), 0), dip2px(getactivity(), 5), 0);
      tuantxt.setlayoutparams(tuantxtparams);
      tuantxt.setbackgroundresource(r.drawable.rectangle_radius_fen);  //设置textview背景图片
      tuantxt.setgravity(gravity.center);
      tuantxt.settext("团");
      txtfour.getpaint().setfakeboldtext(true);
      tuantxt.settextsize(typedvalue.complex_unit_dip, 8);
      tuantxt.settextcolor(color.parsecolor("#ffffff"));
      line_right_text.addview(tuantxt);
      itemmain.addview(line_right_text);
      line_fragment_hot_main.addview(itemmain);
    }
    return view;
  }
  /**
   * 设备像素(dip,dp)转屏幕像素(px)
   * px就是像素,如果用px,就会用实际像素画,比个如吧,用画一条长度为240px的横线,在480宽的模拟器上看就是一半的屏宽,而在320宽的模拟器上看就是2/3的屏宽了。
       * 而dip,就是把屏幕的高分成480分,宽分成320分。比如你做一条160dip的横线,无论你在320还480的模拟器上,都是一半屏的长度。
   * @param context
   * @param dipvalue
   * @return
   */
  public static int dip2px(context context, float dipvalue){
    final float scale = context.getresources().getdisplaymetrics().density;
    return (int)(dipvalue * scale + 0.5f);
  }
  /**
   * 将px值转换为sp值,保证文字大小不变
   *
   * @param pxvalue
   * @param pxvalue
   *      (displaymetrics类中属性scaleddensity)
   * @return
   */
  public static int px2sp(context context, float pxvalue) {
    final float fontscale = context.getresources().getdisplaymetrics().scaleddensity;
    return (int) (pxvalue / fontscale + 0.5f);
  }
  /**
   * 将sp值转换为px值,保证文字大小不变
   *
   * @param spvalue
   * @param spvalue
   *      (displaymetrics类中属性scaleddensity)
   * @return
   */
  public static int sp2px(context context, float spvalue) {
    final float fontscale = context.getresources().getdisplaymetrics().scaleddensity;
    return (int) (spvalue * fontscale + 0.5f);
  }
}

效果图:

Android动态添加设置布局与控件的方法

希望本文所述对大家android程序设计有所帮助。