2011-06-02 20 views
1

我使用LayoutInflater膨脹在視圖的構造視圖的內臟(安卓),具體如下:從視圖構建器內部充氣視圖

public class TrackHeader extends RelativeLayout { 

     public TrackHeader(Context context) { 
      super(context); 
      final LayoutInflater inflater = getLayoutInflater(); 
      final RelativeLayout view = (RelativeLayout) inflater.inflate(
        R.layout.trackheader, this, true); 
      < more here > 
     } 

(我也曾嘗試以下)

 public TrackHeader(Context context) { 
      super(context); 
      final LayoutInflater inflater = getLayoutInflater(); 
      final RelativeLayout view = (RelativeLayout) inflater.inflate(
        R.layout.trackheader, this, false); 
      addView(view, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
      < more here > 
     } 
    } 

的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="match_parent" 
     > 
     <TextView android:id="@+id/trackNumber" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_gravity="center_horizontal" 
      android:textSize="16dip" 
      /> 
     <Button android:id="@+id/waveformBtn" 
      android:layout_width="match_parent" 
      android:layout_height="40dip" 
      android:layout_below="@+id/trackNumber" 
      android:layout_gravity="center_horizontal" 
      android:textSize="12dip"/> 
     <CheckBox android:id="@+id/enabledCheck" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/waveformBtn" 
      android:layout_gravity="center_horizontal" 
      android:textSize="12dip"/> 
     <CheckBox android:id="@+id/loopingCheck" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/enabledBtn" 
      android:layout_gravity="center_horizontal" 
      android:textSize="12dip"/> 
    </RelativeLayout> 

的原因,我誇大它這種方式是t他的類(TrackHeader)是一個嵌套/內部類,這似乎阻止我直接在XML中引用該類。那麼,膨脹時,佈局不起作用。頂部節點(RelativeLayout)的大小正確,以填充TrackHeader,但子視圖根本沒有調整大小並保持爲(0,0,0,0)。好奇,如果有任何Android愛好者在那裏可以推動我在正確的方向...

Thanx提前和快樂的編碼! -E。

回答

0

我不確定這是否說明爲什麼沒有的子視圖大小,但「loopingCheck」元素的「layout_below」屬性可能應該是「@ + id/enabledCheck」而不是「@ + ID/enabledBtn」。除此之外,沒有明顯的問題跳出來。