Android: Dynamic ImageView loading, Radiobutton

Source: Internet
Author: User

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 ",""));
}


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.