Android: Dynamic ImageView loading, Radiobutton
To dynamically load controls, imageview, and radiobutton based on project requirements
Sample:
1. dynamically load imageview
// Display existing images and calculate the number of images
// Here, imgs is the list array of images.
Private void initPhotos (View v, HashMap 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 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 file
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">
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: layout_marginLeft = "2dp"
Android: scaleType = "fitXY"
Android: tag = "@ + tag/img1"/>
Sample2
Dynamic radioButton Loading
Private RadioGroup rg;
Protected void onInitLayout (View view ){
Rg = (RadioGroup) view. findViewById (R. id. qualityRadioGroup );
Rg. setOnCheckedChangeListener (this );
// In fragment, it is more convenient to use receivedpreference to add temporary data.
SharePreference = LSharePreference. getInstance (getActivity ());
// Send json
SendQualityConditionJson ();
}
// Add radiobutton dynamically
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 );
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
// Here, the id is subtracted from 3020, and the data index in the original list is obtained.
SharePreference. setString ("QUALITY_CONDITION ",
MData. get (v. getId ()-3020). id + "");
L. e (sharePreference. getString ("QUALITY_CONDITION ",""));
}