2012-08-22 116 views
2

對不起,如果這充滿了大量問題/答案膨脹,但我無法得到解決我的問題。動態添加視圖到混合xml /代碼複合佈局

我有一個複合視圖(LinearLayout),它具有在XML中定義的固定部分以及代碼中的其他功能。我想動態添加視圖到它。

下面是XML部件(compound.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/compoundView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TextView android:id="@+id/myTextView" 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     android:text="000" /> 
</LinearLayout> 

我已經在代碼中定義的一個的LinearLayout來指代XML:

public class CompoundControlClass extends LinearLayout { 
    public CompoundControlClass (Context context) { 
     super(context); 
     LayoutInflater li; 
     li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     li.inflate(R.layout.compound_xml,*ROOT*, *ATTACH*); 
    } 
    public void addAView(){ 
     Button dynBut = new Button(); 
     // buttoin def+layout info stripped for brevity 
     addView(dynBut); 
    } 
} 

我試圖編程方式添加,以期addAView。

如果根是空的,並附是假的,我有(每HierarchyViewer)以下層次:

  • CompoundControlClass> dynBut

在XML原來的TextView已經一去不復返了。

如果root這並附上是真實的,我有以下層次:

  • CompoundControlClass> compoundView> myTextView
  • CompoundControlClass> dynBut

我想有

  • CompoundControlClass> myTextView
  • CompoundControlClass> dynBut

其中基本上代碼和XML只有一個唯一的View。 我錯過了什麼?

ANSWER從d瑤基於反饋----------------------

訣竅是包括主要佈局的化合物組分,而不是直接引用它。

activity_main.xml中

<RelativeLayout 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"> 
    <include layout="@layout/comound" 
     android:id="@+id/compoundView" 
     android:layout_alignParentBottom="true" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

mainActivity.java

public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     CompoundControlClass c = (CompoundControlClass) this.findViewById(R.id.compoundView); 
     c.addAView(this); 
    } 
} 

CompoundControlClass.java

public class CompoundControlClass extends LinearLayout { 
    public CompoundControlClass(Context context) { 
     super(context); 
    } 
    public CompoundControlClass(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public CompoundControlClass(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void addAView(Context context){ 
     ImageView iv = new ImageView(context);      
     iv.setImageResource(R.drawable.airhorn); 
     addView(iv); 
    } 
} 

compound.xml

<com.sounddisplaymodule.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/compoundView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TextView 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:textSize="40sp" 
     android:textStyle="bold" 
     android:text="0:00" />  
</com.sounddisplaymodule.CompoundControlClass> 

回答

0

爲什麼不直接在線性佈局上調用addView?根據您列出的需求,我沒有看到CompoundControlClass的需求。

LinearLayout v = (LinearLayout)findViewById(R.id.compoundView); 
v.addView(dynBut); 

在這種情況下,v將包含myTextView,然後是dynBut。

如果您希望添加的其他功能,而真正感覺到有必要創建複合控件類,見好就收構造爲超(等),並刪除其餘

那麼你的XML是這樣的:

<com.yourpackage.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/compoundView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TextView android:id="@+id/myTextView" 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     android:text="000" /> 
</com.yourpackage.CompoundControlClass> 

您還必須確保您的CompoundControlClass.java包含適當的構造函數,該構造函數同時使用Context和屬性集。

然後,在你的java,你叫的setContentView後,您可以執行以下操作:

CompoundControlClass c = (CompoundControlClass)findViewById(R.id.compoundView); 
Button b = new Button(context); 
//setup b here or inflate your button with inflater 
c.addView(b); 

這將給你你想要的層次結構。

+0

這就是我最初所做的事情,但在向LinearLayout添加更多邏輯(稱爲v)後,我需要一種方法將其與活動隔​​離。這就是爲什麼我開始將該邏輯集成到CompoundControlClass – Breiz

+0

可以將它實現爲LinearLayout的擴展。那麼,在你的xml中只需簡單地包含你的擴展類。看到我上面的編輯 –

+0

這非常有道理。我提出了建議的更改,刪除了版面充氣器並跑了。我確實有動態按鈕,但固定的XML不在那裏。我再次檢查HierarchyViewer,我有CompoundControlClass> dynBut和沒有TextView。 – Breiz