簡體   English   中英

如何將 ConstraintLayout 添加到 LinearLayout - 以編程方式 [Android]

[英]How to add a ConstraintLayout to a LinearLayout - programmaticaly [Android]

正如標題所述,我確實有兩個元素:

  1. 線性布局:

     <LinearLayout android:id="@+id/idContent" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.384" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.419" android:orientation="horizontal"> </LinearLayout>
  2. 約束布局

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res `/android"` xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/idExample"> <EditText android:id="@+id/iddas" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="date" /> </androidx.constraintlayout.widget.ConstraintLayout>

問題:如何將ConstraintLayout添加到 LinearLayout?

我嘗試了什么:

LinearLayout linear = (LinearLayout) findViewById(R.id.idContent);
ConstraintsLayout cst = (ConstraintsLayout) findViewById(R.id.idExample);
linear.addView(cst);

問題:錯誤:無法將 null 子視圖添加到 ViewGroup

我猜它是null因為它從未被充氣過。 假設包含ConstraintsLayout的布局是my_layout.xml ,現在您應該先對其進行充氣,然后將其添加到LinearLayout

LayoutInflater layoutInflater = LayoutInflater.from(context);
View cst = layoutInflater.inflate(R.layout.my_layout, null);
linear.addView(cst);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM