2012-05-17 28 views
0

我試圖在RelativeLayout上找到一個句柄,該句柄定義應該如何佈局ListView中的一行。我越來越空,我相信這是因爲兩件事之一。嘗試將樹移到ListView中的RelativeLayout

可能性之一是它沒有膨脹,所以它返回null ....雖然我讀過關於這一個,但我對通脹概念很不滿意。現在我已經用Java編程了3周(來自C#)。 所以我不知道如何測試,更不用說處理。舉例來說,我所做的就是將適配器設置爲列表視圖....不知道什麼事件或何時發生了通貨膨脹。

可能性二是我沒有正確地走樹。

我的父母:

<RelativeLayout 
    android:minHeight="320dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/rlay_LftDataParent" 
    android:orientation="vertical" > 

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/lstvw_LiftData" /> 

行定義

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/rlay_LftDataRw" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background"> 
... 
</RelativeLayout> 

在我試過活動(我想設置最小高度爲佈局):( lstvw_LiftData是我的ListView對象)

RelativeLayout RowLayout = (RelativeLayout)lstvw_LiftData.findViewById(R.id.rlay_LftDataRw); 

RelativeLayout RowLayout = (RelativeLayout)findViewById(R.id.rlay_LftDataRw); 

如果你想知道的大局觀,這是對問題的後續從這個帖子Using RelativeLayout to control a ListView row height在那裏我試圖讓該行佈局以填充屏幕。

回答

1

您不能從ListViewfindViewById訪問View。請使用適配器在getView方法中獲取RelativeLayout的引用,並在其中設置寬度和高度。你沒有說你用什麼適配器,所以這裏是基本ArrayAdapter一個簡單的例子:

編輯: 連你自己都不叫getView()方法,該適配器將自動調用getView方法時,它的時間顯示一個新的行。關於您上次評論的問題,如果自定義Adapter類是Activity一個內部類,你會使用它:

public class SomeActivity extends ListActivity { 

    private int mHeight, mWidth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // ...do what ever stuff you need in this activity 
     // find out the height/width values: 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     mHeight = metrics.heightPixels; 
     mWidth = metrics.widthPixels; 
     // set the adapter 
     setListAdapter(new CustomAdapter(this, 
       R.layout.adapters_listview_rowheight, R.id.textView1, items)); 
    } 

    //you use your own adapter class 
    private class CustomAdapter extends ArrayAdapter<String> { 

     public CustomAdapter(Context context, int resource, 
       int textViewResourceId, String[] objects) { 
      super(context, resource, textViewResourceId, objects); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
        //do whatever logic you need here 
        // finally you end up with the RelativeLayout you'll return as the view for the row 
        // before you return set the desired values for its width and height 
      rl.getLayoutParams().height = mHeight; 
      rl.getLayoutParams().width = mWidth; 
      return rl; 
     } 


    } 

} 

如果Adapter類是在另一個文件中,然後讓該適配器的構造函數取表示的寬度和高度的兩個int值:

//the constructor 
public CustomAdapter(/*whatever parameters you already have*/, int height, int width) { 
    //do stuff 
    //now you also have a reference for the width and height that you can use in the getView method. 
} 

並在Activity(與用於onCreate方法相同的碼):

setListAdapter(new CustomAdapter(/*whatever parameters you already have*/, mHeight, mWidth)); 

我不知道這是你想要的。

+0

我正在追蹤除params之外的所有內容。我正在使用一個自定義的適配器,以便您的示例運行良好,我只是不知道要傳遞給getView覆蓋。在我的活動中,我調用RelativeLayout X =(RelativeLayout)LiftDataAdapter.getView(position,convertView,parent);這將打我的重寫我只是不知道的參數?在一個單獨的不是你聲明使用mHeight這很好,除非我不知道如何從活動範圍獲取該參數到自定義適配器範圍。在調用getView之前是否創建了一個方法來傳遞它? – GPGVM

+1

好吧,我現在明白了。當系統填充getView時,系統會調用getView,因此我的問題仍然是如何將活動範圍中的值傳遞到適配器作用域。 – GPGVM

+0

@ user1278561看我編輯的答案。 – Luksprog

0

您通過Adapter訪問您的行。在getView之類的東西中,但取決於您使用的Adapter,它可能會有所不同。