對不起,如果這充滿了大量問題/答案膨脹,但我無法得到解決我的問題。動態添加視圖到混合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>
這就是我最初所做的事情,但在向LinearLayout添加更多邏輯(稱爲v)後,我需要一種方法將其與活動隔離。這就是爲什麼我開始將該邏輯集成到CompoundControlClass – Breiz
可以將它實現爲LinearLayout的擴展。那麼,在你的xml中只需簡單地包含你的擴展類。看到我上面的編輯 –
這非常有道理。我提出了建議的更改,刪除了版面充氣器並跑了。我確實有動態按鈕,但固定的XML不在那裏。我再次檢查HierarchyViewer,我有CompoundControlClass> dynBut和沒有TextView。 – Breiz