android - 如何以编程方式添加到布局复合组件 Android?

标签 android android-layout view custom-component

我创建了一个复合组件 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 类:

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();。怎么做?

最佳答案

我建议您还实现仅采用 ContextLinearLayout 的构造函数:

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

关于android - 如何以编程方式添加到布局复合组件 Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10372451/

相关文章:

android - 如何让按钮按下一次然后不再按下?

android - 尝试在没有父 View 的情况下复制 View - Android

java - moveToNext() 为假

Android Studio 无法解析导入模块中的符号

java - 如何设置圆形不确定进度条android的粗细?

android - 按钮布局均匀填充屏幕大小

SimpleCursorAdapter 上的安卓过滤器

android - 设置 3 个搜索栏以更改 RGB channel

Android 3D按钮文字位置

iphone - UITabBarController - 如何为简单的开关设置动画?