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

问题描述

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

主活动代码:-

       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>

输出

enter image description here

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

解决方法

您的 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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...