以编程方式在膨胀布局中添加视图

Programmatically Adding views in inflated layout

我正在尝试以编程方式在线性布局中添加视图,这是膨胀视图的一部分,但在线性布局中添加视图后它不显示

MainActivity 代码:-

       final LayoutInflater inflater = this.getLayoutInflater();
            View view = inflater.inflate(R.layout.suscription_alert,null);
            LinearLayout ll = view.findViewById(R.id.LinearLayout);

       for(int i =0;i<4;i++) {
                TextView t = new TextView(MainActivity.this);
                t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                t.setText("Hello");
                ll.addView(t);
            }
            view.invalidate();

        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .setView(view)
                .create();
        Objects.requireNonNull(alert.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);

        alert.show();

订阅警报布局:-

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="SUBSCRIPTIONS"
        android:textColor="#F5D90A"
        android:textSize="23sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:paddingTop="10dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

输出:

以编程方式添加的视图未在此处显示, 如何解决这个问题以及为什么会发生这种情况有人知道吗?

您的 LinearLayout 的高度需要大于 0 才能在 运行 时添加视图,否则它将无法计算高度。

例如wrap_content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="SUBSCRIPTIONS"
        android:textColor="#F5D90A"
        android:textSize="23sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>