![](/img/trans.png)
[英]How to create LinearLayout with ImageView and add it to another LinearLayout programatically
[英]Programatically add multiples ImageView to LinearLayout
我的應用程序有問題。 我正在嘗試在我的linearlayout內部生成多個imageview。 圖像必須由我的for循環創建(我從JSONArray獲取值)。 我沒有錯誤,但是我的設備上沒有任何顯示。 我不知道我的錯在哪里。
這是我的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/layout_netdetails"
tools:context="ch.gaspardrosay.testapp.NetStatusDetails">
</LinearLayout>
這是我的java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net_status_details);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_netdetails);
iIdStatus = getIntent().getExtras().getString("netdata_suivi");
try{
JSONArray jsArray = new JSONArray(iIdStatus);
for(int i = 0; i < jsArray.length(); i++){
JSONObject tempJson = jsArray.getJSONObject(i);
// Traitement
String sStatus = (String)tempJson.get("status_suivi");
String sSuivi = (String)tempJson.get("suivi");
int iNoStatus = Integer.parseInt(sStatus);
switch(iNoStatus){
case 1:
sStatus = "red.png";
break;
case 2:
sStatus = "orange.png";
break;
case 3:
sStatus = "green.png";
break;
case 4:
sStatus = "blue.png";
break;
case 5:
sStatus = "green-orange.png";
break;
}
Log.i("Status image", sStatus );
ImageView imageView = new ImageView(NetStatusDetails.this);
ViewGroup.LayoutParams imageViewLayoutParams = new ViewGroup.LayoutParams(20,20);
imageView.setLayoutParams(imageViewLayoutParams);
imageView.setBackgroundResource(ITToolsMainActivity.GET_RESOURCES.getIdentifier(sStatus, "drawable", ITToolsMainActivity.PACKAGE_NAME));
layout.addView(imageView);
}
}
catch(JSONException e){
e.printStackTrace();
}
}
謝謝您的幫助 !
編輯
我已經按照建議嘗試過Hierarchyviewer。 無法獲得任何結果,這是我得到的:
好的,我發現了自己的錯誤...一切正常,在獲取資源之前,我只是在每個資源名稱上添加了“ .png”。 刪除了它們,現在工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.