Android for dynamic load Imageview,radiobutton

Source: Internet
Author: User

Depending on the project requirements, you want to load the control dynamically, Imageview,radiobutton


Sample

1. Dynamic load ImageView

Show existing pictures, calculate number of pictures

Here IMGs is a list array of pictures
private void Initphotos (View V, Hashmap<integer, string> IMGs) {
Linearlayout.layoutparams LP = new Linearlayout.layoutparams (
(GETWIDTHPX ()-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 file



<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

Dynamic load RadioButton



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 add temporary data using shareedpreference
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, 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
The ID, minus 3020, is the data index in the original list.
Sharepreference.setstring ("Quality_condition",
Mdata.get (V.getid ()-3020). ID + "");
L.E (sharepreference.getstring ("Quality_condition", ""));
}


Android for dynamic load Imageview,radiobutton

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.