2014-05-02 152 views
0

我目前正在製作一個使用動態佈局的應用程序。這是我第一次做這個,我只是爲了學習它而做的。我設法讓它加載大部分數據並正確顯示,但我遇到了第一項問題。由於某種原因,它跳過它並從第二項開始。它將它加載到一個變量中,但由於某種原因它未被顯示。我相信它與佈局參數有關,但我找不到這樣做的理由。在Android中加載動態佈局

這是它的外觀(注意,它應該141-開始......項目,它被裝載到一個變量壽卻未顯示)

enter image description here

這是我的代碼中號使用根據

for (int i = 0; i <= id_l; i++) 
      { 
       RelativeLayout container = (RelativeLayout)findViewById(R.id.popisLinija); 
       View item_layout = getLayoutInflater().inflate(R.layout.item_layout, null); 
       Integer l = id_l; 
       Log.d("id length: ", l.toString()); 
       Log.d("ID: ", id[i].toString()); 
       Log.d("Linija br: ", broj_linije[i]); 
       Integer i2 = i; 
       Log.d("i= ", i2.toString()); 

       item_layout.setId(i); 
       item_layout.setTag("View"); 

       final Integer h = item_layout.getId(); 

       // fill in any details dynamically here 
       assert item_layout != null; 
       TextView textView = (TextView) item_layout.findViewById(R.id.item_text); 

       if(broj_linije[i].equals("000")) 
       { 
        textView.setText(naziv_linije[i]); 
       } 
       else 
       { 
        textView.setText(broj_linije[i] + " - " + naziv_linije[i]); 
       } 


       textView.setTag("Text"); 


       RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
       if(i == 0) 
       { 
        if (rlp != null) { 
         rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, container.getId()); 
        } 
       } 
       if(i > 0) 
       { 
        if (rlp != null) { 
         rlp.addRule(RelativeLayout.BELOW, i-1); 
        } 
       } 


       FrameLayout item_button = (FrameLayout) item_layout.findViewById(R.id.item_button); 
       item_button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View arg0) 
        { 
         Integer a = h; 
         Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show(); 
         Log.d("clicked", h.toString()); 
        } 
       }); 

       item_layout.setLayoutParams(rlp); 
       container.addView(item_layout); 
      } 

我已經檢查了變量加載的項目,如果該項目被裝載

> 05-02 07:09:00.116 21204-21204/com.example.app D/ID:﹕ 0 05-02 <<< ITEM 141 VARIABLE 
> 07:09:00.116 21204-21204/com.example.app D/Linija br:﹕ 141 05-02 <<< ITEM 141 VARIABLE 
> 07:09:00.116 21204-21204/com.example.app D/i=﹕ 0 05-02 07:09:00.166 <<< ITEM 141 VARIABLE 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.166 
> 21204-21204/com.example.app D/ID:﹕ 1 05-02 07:09:00.166 
> 21204-21204/com.example.app D/Linija br:﹕ 142 05-02 07:09:00.166 
> 21204-21204/com.example.app D/i=﹕ 1 05-02 07:09:00.176 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.176 
> 21204-21204/com.example.app D/ID:﹕ 2 05-02 07:09:00.176 
> 21204-21204/com.example.app D/Linija br:﹕ 143 05-02 07:09:00.176 
> 21204-21204/com.example.app D/i=﹕ 2 05-02 07:09:00.206 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.206 
> 21204-21204/com.example.app D/ID:﹕ 3 05-02 07:09:00.206 
> 21204-21204/com.example.app D/Linija br:﹕ 144 05-02 07:09:00.206 
> 21204-21204/com.example.app D/i=﹕ 3 05-02 07:09:00.216 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.216 
> 21204-21204/com.example.app D/ID:﹕ 4 05-02 07:09:00.216 
> 21204-21204/com.example.app D/Linija br:﹕ 145 05-02 07:09:00.216 
> 21204-21204/com.example.app D/i=﹕ 4 05-02 07:09:00.226 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.226 
> 21204-21204/com.example.app D/ID:﹕ 5 05-02 07:09:00.226 
> 21204-21204/com.example.app D/Linija br:﹕ 146 05-02 07:09:00.226 
> 21204-21204/com.example.app D/i=﹕ 5 05-02 07:09:00.246 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.246 
> 21204-21204/com.example.app D/ID:﹕ 6 05-02 07:09:00.246 
> 21204-21204/com.example.app D/Linija br:﹕ 147 05-02 07:09:00.246 
> 21204-21204/com.example.app D/i=﹕ 6 05-02 07:09:00.276 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.276 
> 21204-21204/com.example.app D/ID:﹕ 7 05-02 07:09:00.276 
> 21204-21204/com.example.app D/Linija br:﹕ 148 05-02 07:09:00.276 
> 21204-21204/com.example.app D/i=﹕ 7 05-02 07:09:00.316 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.316 
> 21204-21204/com.example.app D/ID:﹕ 8 05-02 07:09:00.316 
> 21204-21204/com.example.app D/Linija br:﹕ 149 05-02 07:09:00.316 
> 21204-21204/com.example.app D/i=﹕ 8 05-02 07:09:00.316 
> 21204-21208/com.example.app D/dalvikvm﹕ GC_CONCURRENT freed 949K, 37% 
> free 7958K/12524K, paused 9ms+17ms, total 92ms 05-02 07:09:00.326 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.326 
> 21204-21204/com.example.app D/ID:﹕ 9 05-02 07:09:00.326 
> 21204-21204/com.example.app D/Linija br:﹕ 150 05-02 07:09:00.326 
> 21204-21204/com.example.app D/i=﹕ 9 05-02 07:09:00.336 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.336 
> 21204-21204/com.example.app D/ID:﹕ 10 05-02 07:09:00.336 
> 21204-21204/com.example.app D/Linija br:﹕ 150A 05-02 07:09:00.336 
> 21204-21204/com.example.app D/i=﹕ 10 05-02 07:09:00.346 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.346 
> 21204-21204/com.example.app D/ID:﹕ 11 05-02 07:09:00.346 
> 21204-21204/com.example.app D/Linija br:﹕ 151 05-02 07:09:00.346 
> 21204-21204/com.example.app D/i=﹕ 11 05-02 07:09:00.366 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.366 
> 21204-21204/com.example.app D/ID:﹕ 12 05-02 07:09:00.366 
> 21204-21204/com.example.app D/Linija br:﹕ 153 05-02 07:09:00.366 
> 21204-21204/com.example.app D/i=﹕ 12 05-02 07:09:00.376 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.376 
> 21204-21204/com.example.app D/ID:﹕ 13 05-02 07:09:00.376 
> 21204-21204/com.example.app D/Linija br:﹕ 155 05-02 07:09:00.376 
> 21204-21204/com.example.app D/i=﹕ 13 05-02 07:09:00.386 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.386 
> 21204-21204/com.example.app D/ID:﹕ 14 05-02 07:09:00.386 
> 21204-21204/com.example.app D/Linija br:﹕ 156 05-02 07:09:00.386 
> 21204-21204/com.example.app D/i=﹕ 14 05-02 07:09:00.396 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.396 
> 21204-21204/com.example.app D/ID:﹕ 15 05-02 07:09:00.396 
> 21204-21204/com.example.app D/Linija br:﹕ 157 05-02 07:09:00.396 
> 21204-21204/com.example.app D/i=﹕ 15 05-02 07:09:00.406 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.406 
> 21204-21204/com.example.app D/ID:﹕ 16 05-02 07:09:00.406 
> 21204-21204/com.example.app D/Linija br:﹕ 158 05-02 07:09:00.406 
> 21204-21204/com.example.app D/i=﹕ 16 05-02 07:09:00.416 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.416 
> 21204-21204/com.example.app D/ID:﹕ 17 05-02 07:09:00.416 
> 21204-21204/com.example.app D/Linija br:﹕ 159 05-02 07:09:00.416 
> 21204-21204/com.example.app D/i=﹕ 17 05-02 07:09:00.426 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.426 
> 21204-21204/com.example.app D/ID:﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 160 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.436 
> 21204-21204/com.example.app D/ID:﹕ 19 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 162 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 19 05-02 07:09:00.446 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.446 
> 21204-21204/com.example.app D/ID:﹕ 20 05-02 07:09:00.446 
> 21204-21204/com.example.app D/Linija br:﹕ 260 05-02 07:09:00.446 
> 21204-21204/com.example.app D/i=﹕ 20 05-02 07:09:00.456 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.456 
> 21204-21204/com.example.app D/ID:﹕ 21 05-02 07:09:00.456 
> 21204-21204/com.example.app D/Linija br:﹕ 260A 05-02 07:09:00.456 
> 21204-21204/com.example.app D/i=﹕ 21 05-02 07:09:00.476 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.476 
> 21204-21204/com.example.app D/ID:﹕ 22 05-02 07:09:00.476 
> 21204-21204/com.example.app D/Linija br:﹕ 261 05-02 07:09:00.476 
> 21204-21204/com.example.app D/i=﹕ 22 05-02 07:09:00.486 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.486 
> 21204-21204/com.example.app D/ID:﹕ 23 05-02 07:09:00.486 
> 21204-21204/com.example.app D/Linija br:﹕ 262 05-02 07:09:00.486 
> 21204-21204/com.example.app D/i=﹕ 23 05-02 07:09:00.496 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.496 
> 21204-21204/com.example.app D/ID:﹕ 24 05-02 07:09:00.496 
> 21204-21204/com.example.app D/Linija br:﹕ 263 05-02 07:09:00.496 
> 21204-21204/com.example.app D/i=﹕ 24 05-02 07:09:00.506 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.506 
> 21204-21204/com.example.app D/ID:﹕ 25 05-02 07:09:00.506 
> 21204-21204/com.example.app D/Linija br:﹕ 264 05-02 07:09:00.506 
> 21204-21204/com.example.app D/i=﹕ 25 05-02 07:09:00.516 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.516 
> 21204-21204/com.example.app D/ID:﹕ 26 05-02 07:09:00.516 
> 21204-21204/com.example.app D/Linija br:﹕ 265 05-02 07:09:00.516 
> 21204-21204/com.example.app D/i=﹕ 26 05-02 07:09:00.526 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.526 
> 21204-21204/com.example.app D/ID:﹕ 27 05-02 07:09:00.526 
> 21204-21204/com.example.app D/Linija br:﹕ 266 05-02 07:09:00.526 
> 21204-21204/com.example.app D/i=﹕ 27 05-02 07:09:00.536 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.536 
> 21204-21204/com.example.app D/ID:﹕ 28 05-02 07:09:00.536 
> 21204-21204/com.example.app D/Linija br:﹕ 267 05-02 07:09:00.536 
> 21204-21204/com.example.app D/i=﹕ 28 05-02 07:09:00.546 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.546 
> 21204-21204/com.example.app D/ID:﹕ 29 05-02 07:09:00.546 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.546 
> 21204-21204/com.example.app D/i=﹕ 29 05-02 07:09:00.556 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.556 
> 21204-21204/com.example.app D/ID:﹕ 30 05-02 07:09:00.556 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.556 
> 21204-21204/com.example.app D/i=﹕ 30 05-02 07:09:00.566 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/ID:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.566 
> 21204-21204/com.example.app D/i=﹕ 31 

任何人都可以幫我解決我在這裏做錯了什麼?我將不勝感激,我在這個嘗試各種各樣的東西沒有結果的時候失去了幾個小時。

編輯:

我已經加入我的佈局XML文件

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:id="@+id/item_container" 
    android:clickable="false" 
    android:focusable="false"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:padding="2dp" 
     android:background="#e8e8e8" 
     android:id="@+id/item_body" 
     android:clickable="false" 
     android:focusable="false"> 

     <FrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/background" 
      android:id="@+id/item_button" 
      android:clickable="true" 
      android:longClickable="true" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:minHeight="55dp"> 

       <FrameLayout 
        android:layout_width="3dp" 
        android:layout_height="fill_parent" 
        android:background="@android:color/transparent" 
        android:id="@+id/item_indicator"></FrameLayout> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="141-SAMOBOR-RAKOV POTOK-JAGNJIĆ DOL" 
        android:id="@+id/item_text" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="8dp" 
        android:layout_marginRight="30dp" 
        android:textAppearance="@android:style/TextAppearance.Small" 
        android:gravity="fill" /> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/item_icon" 
        android:layout_gravity="center_vertical|right" 
        android:background="@android:color/transparent" 
        android:src="@drawable/plus_t" 
        android:layout_marginRight="5dp" 
        android:contentDescription="Proširi" /> 

      </FrameLayout> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/item_image_container" 
      android:layout_below="@+id/item_body" 
      android:layout_marginTop="5dp" 
      android:background="#ffffff"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/item_image" 
       android:background="@android:color/transparent" 
       android:layout_centerHorizontal="true" 
       android:scaleType="fitCenter" /> 
     </RelativeLayout> 

</RelativeLayout> 

回答

2

嘗試這種方式,希望這將幫助你......

XML代碼

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="180dp" 
     android:src="@drawable/ic_launcher" 
     android:adjustViewBounds="true"/> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <LinearLayout 
      android:id="@+id/lnrDynamicItem" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

活動代碼

MainActivity

public class MainActivity extends Activity { 

    private LinearLayout lnrDynamicItem; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     lnrDynamicItem = (LinearLayout) findViewById(R.id.lnrDynamicItem); 

     for (int i = 0; i <= id_l; i++) 
     { 
      Integer l = id_l; 
      Log.d("id length: ", l.toString()); 
      Log.d("ID: ", id[i].toString()); 
      Log.d("Linija br: ", broj_linije[i]); 
      Integer i2 = i; 
      Log.d("i= ", i2.toString()); 

      item_layout.setId(i); 
      item_layout.setTag("View"); 


      TextView textView = new TextView(this); 
      textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      textView.setId(i+1); 

      if(broj_linije[i].equals("000")) 
      { 
       textView.setText(naziv_linije[i]); 
      } 
      else 
      { 
       textView.setText(broj_linije[i] + " - " + naziv_linije[i]); 
      } 


      textView.setTag(textView.getId()); 



      textView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) 
       { 
        Integer a = (Integer) view.getTag(); 
        Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show(); 
        Log.d("clicked", h.toString()); 
       } 
      }); 

      lnrDynamicItem.addView(textView); 
     } 

    } 

} 
+0

謝謝。我採取了你的方法,並將線性佈局添加到現有的相對佈局,並刪除了添加子規則的部分代碼。現在它正確顯示所有項目。 –