Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓 有關動態加載ImageView,Radiobutton

安卓 有關動態加載ImageView,Radiobutton

編輯:關於Android編程

根據項目需求,想要動態加載控件,imageview,radiobutton


sample:

1.動態加載imageview

// 顯示已有的圖片, 計算圖片數量

//這裡imgs是圖片的list數組
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 < 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文件



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

動態加載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", ""));
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved