首页 > 代码库 > 安卓 有关动态加载ImageView,Radiobutton

安卓 有关动态加载ImageView,Radiobutton

根据项目需求,想要动态加载控件,imageview,radiobutton


sample:

1.动态加载imageview

// 显示已有的图片, 计算图片数量

       //这里imgs是图片的list数组
private void initPhotos(View v, HashMap<Integer, String> imgs) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(getWidthPx() - 12 * 2) / 3, (getWidthPx() - 20 * 2) / 4);


int imgCount = (imgs == null || imgs.size() == 0) ? 0 : (imgs.size() > 3 ? 3 : imgs.size());
for (int i = 0; i < imgCount; i++) {
ImageView img = (ImageView) v.findViewWithTag("img" + i);
MApplication.get().getImageLoader2().displayImage(imgs.get(i), img);
img.setVisibility(View.VISIBLE);
img.setLayoutParams(lp);
}
imgLayout1.setLayoutParams(lp);
imgLayout2.setLayoutParams(lp);
imgLayout3.setLayoutParams(lp);
}


xml文件



  <LinearLayout
                         android:id="@+id/level_imglayout2d"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/levelup_udlr"
                        android:gravity="center_vertical"
                        android:orientation="horizontal"
                        android:padding="3dp"
                        android:visibility="invisible" >


                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="2dp"
                            android:scaleType="fitXY"
                            android:tag="@+tag/img1" />
                    </LinearLayout>


sample2

动态加载radioButton



private RadioGroup rg;
protected void onInitLayout(View view) {


rg = (RadioGroup) view.findViewById(R.id.qualityRadioGroup);


rg.setOnCheckedChangeListener(this);
     //在fragment中,使用shareedPreference添加临时数据更方便
sharePreference = LSharePreference.getInstance(getActivity());
 
//发送json
sendQualityConditionJson();


}
   //动态 添加radiobutton
private void setData() {


for (int i = 0; i < mData.size(); i++) {
final RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(mData.get(i).name);
radioButton.setTextColor(Color.BLACK);
radioButton.setButtonDrawable(R.drawable.radio_button_selector);

radioButton.setPadding(55, 10, 10, 10);
radioButton.setTag(i);
radioButton.setId(3020 + i);
if(!LFormat.isEmpty(sharePreference.getString("QUALITY_CONDITION"))){
if (i == Integer.parseInt(sharePreference.getString("QUALITY_CONDITION"))-3020-2){
radioButton.setChecked(true);
}
}else if (i==0){
radioButton.setChecked(true);
}
rg.addView(radioButton);


radioButton.setOnClickListener(this);
}
rg.setOnCheckedChangeListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//这里把id,又减去了3020,得到的就是原始的list中的数据索引
sharePreference.setString("QUALITY_CONDITION",
mData.get(v.getId() - 3020).id + "");
L.e(sharePreference.getString("QUALITY_CONDITION", ""));
}


安卓 有关动态加载ImageView,Radiobutton