How to add to the layout compound component programmatically Android?

痴心易碎 提交于 2019-12-10 10:36:58

问题


I created a compound component Box which I want to add to the layout. Box xml:

<?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 class:

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);
    }
}

Everything works if I add the Box to the main xml file like:

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

The problem is that I'd like to create many of boxes programmatically something like this: Box mybox= new Box();. How to do it?


回答1:


I would suggest you also implement the constructors of the LinearLayout that takes only a Context:

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

Then in your Activity, when you want to add a new Box, instantiate the Box class and add it to your layout:

// 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);


来源:https://stackoverflow.com/questions/10372451/how-to-add-to-the-layout-compound-component-programmatically-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!