2

我创建了一个Box要添加到布局中的复合组件。Boxxml:

<?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 添加到主 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();. 怎么做?

4

1 回答 1

9

我建议你也实现LinearLayout只需要 a的构造函数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);
于 2012-04-29T18:44:10.327 回答