2012-04-29 28 views
2

我創建了一個複合組件Box,我想添加到佈局中。 Box XML:如何以編程方式添加到佈局複合組件Android?

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayoutForBlock" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:background="@drawable/screen_background" android:layout_marginLeft="5dp" android:layout_marginTop="5dp"> 
     <ImageButton 
      android:id="@+id/imageButtonContent" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_gravity="center_horizontal" 
      android:scaleType="fitCenter" 
      android:src="@drawable/beach_bed" android:background="@drawable/buttonbackground" android:clickable="true" android:layout_margin="5dp" android:contentDescription="@string/sample_text"/> 
     <TextView 
      android:id="@+id/textViewContent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:text="@string/sample_text" 
      android:textColor="@color/deep_blue" android:layout_margin="5dp"/> 
    </LinearLayout> 
</merge> 

Box類:

public class Box extends LinearLayout { 

    private TextView textContent; 
    private ImageView imageContent; 

    public Box(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     ((Activity)getContext()).getLayoutInflater().inflate(R.layout.box, this); 
     setupViewItems(); 
    } 

    private void setupViewItems() { 
     textContent = (TextView) findViewById(R.id.textViewContent); 
     imageContent = (ImageView) findViewById(R.id.imageButtonContent); 
    } 

    public void setTextContent(String text) { 
     this.textContent.setText(text); 
    } 
    public void setImageContent(String tag) { 
     this.imageContent.setContentDescription(tag); 
    } 
} 

一切正常,如果我添加了Box像主要的XML文件:

<com.mypackage.alexey.Box 
android:id="@+id/mybox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
/> 

的問題是,我想以編程方式創建許多框,如下所示:Box mybox= new Box();。怎麼做?

回答

8

我建議你也實現,是以LinearLayout的構造函數只有一個Context

public Box(Context context) { 
    super(context); 
} 

然後在你的Activity,當你想添加一個新的Box,實例化Box類並將其添加到你的佈局:

// I assumed you have a LinearLayout to which you want to add your Box 
LinearLayout parent = (LinearLayout) findViewById(R.id.parent_id); 
//create the Box and added to the parent above 
Box theBox = new Box(this); // if you are in an Activity 
//some LayoutParams to replicate your xml attributes 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
// set the Orientation for the Box 
theBox.setOrientation(LinearLayout.HORIZONTAL); 
//add the Box 
parent.addView(theBox); 
+0

謝謝你的回答,但是當我嘗試賦值時,有一個問題:theBox.setTextContent(「New content」);我得到異常(NullPointerException) – Alex 2012-04-30 08:47:04

+1

@alexey當你實例化Box類和'theBox'是一個有效的對象,這意味着'NullPointerException'可能來自'textContent'爲'null'(它在佈局中找不到)。我不知道如何調用'onFinishInflate'(但我懷疑它適用於xml佈局),我建議您將代碼移動到您爲'R.layout.box'文件充氣並設置視圖('setupViewItems()')在構造函數中使用一個簡單的'Context'並重試。也可以使用這個版本的'inflate':'inflate(R.layout.box,this,true);' – Luksprog 2012-04-30 09:05:23

+0

我照你的建議做了,現在可以運行了,非常感謝! – Alex 2012-04-30 09:47:36

相關問題