安卓 有關動態載入ImageView,Radiobutton

來源:互聯網
上載者:User

標籤:android   style   color   io   os   使用   ar   for   檔案   

根據項目需求,想要動態載入控制項,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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.