【问题标题】:Programmatically Adding views in inflated layout以编程方式在膨胀布局中添加视图
【发布时间】:2021-08-02 22:26:12
【问题描述】:

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

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>

输出:

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

【问题讨论】:

    标签: android android-linearlayout android-view android-inflate


    【解决方案1】:

    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>
    

    【讨论】:

    • 谢谢哥们,我不敢相信我犯了这么愚蠢的错误,浪费了我几个小时?程序员的生命
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    相关资源
    最近更新 更多