gpt4 book ai didi

java - 以编程方式在垂直 LinearLayout 内添加约束布局?

转载 作者:行者123 更新时间:2023-12-02 06:20:00 24 4
gpt4 key购买 nike

我正在尝试在我的片段中动态添加布局。我的主要容器是 ScrollView 内的 LinearLayout。该 Activity 处理带有选项卡布局的 View 页面。

我正在尝试在我的片段中添加 calc_clothers_washer.xml 的 ContraintLayout。但是,所有约束和尺寸都会重置,因此所有 View 都会放置在屏幕的左上角。我尝试过强制转换并使用 View 来替换它,但结果仍然相同。这是迄今为止我得到的最接近的结果。

这是片段


private ConstraintLayout calcMeasureView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

View view = inflater.inflate(
R.layout.calc_base_measure_layout,
container, false);
ConstraintLayout baseCalcContainer = container.findViewById(R.id.base_measure_container);
int calcId = R.layout.calc_clothes_washer;
calcMeasureView = (ConstraintLayout)inflater
.inflate(calcId, null);
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

LinearLayout linearLayout = view.findViewById(R.id.calc_main_container);

ConstraintLayout baseCalcContainer = view.findViewById(R.id.base_measure_container);

linearLayout.removeView(baseCalcContainer);
linearLayout.addView(calcMeasureView, 0);

}

这是我的 calc_base_measure_layout.xml

<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ndroid="http://schemas.android.com/apk/res-auto"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/calc_main_container">

<android.support.constraint.ConstraintLayout
android:id="@+id/base_measure_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/blue"/>

<include
layout="@layout/calc_common_container" />

<include
android:id="@+id/include"
layout="@layout/measure_results_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>

</LinearLayout>

</ScrollView>

这是 calc_clothes_washer.xml

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ndroid="http://schemas.android.com/tools"
android:background="@drawable/background"
android:id="@+id/clothes_washer_container"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<!-- How is the water heated? -->
<TextView
android:id="@+id/heater_source_label"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:text="Water Heating Source"
android:textColor="@color/white"
ndroid:layout_constraintStart_toStartOf="parent"
ndroid:layout_constraintTop_toTopOf="parent" />

<Spinner
style="@style/SpinnerTheme"
android:id="@+id/water_heating_source_dropdown"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_width="0dp"
ndroid:layout_constraintEnd_toStartOf="@+id/water_heating_source_about"
ndroid:layout_constraintStart_toStartOf="@+id/heater_source_label"
ndroid:layout_constraintTop_toBottomOf="@+id/heater_source_label" />

<ImageView
android:id="@+id/water_heating_source_about"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_width="wrap_content"
ndroid:layout_constraintBottom_toBottomOf="@+id/water_heating_source_dropdown"
ndroid:layout_constraintEnd_toEndOf="parent"
ndroid:srcCompat="@android:drawable/ic_menu_info_details" />

<!-- How is the Dryer Heated? -->
<TextView
android:id="@+id/textView3"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:text="Dryer Heating Source"
android:textColor="@color/white"
ndroid:layout_constraintStart_toStartOf="parent"
ndroid:layout_constraintTop_toBottomOf="@+id/water_heating_source_dropdown" />

<Spinner
style="@style/SpinnerTheme"
android:id="@+id/dryer_heating_source_dropdown"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_width="0dp"
ndroid:layout_constraintEnd_toStartOf="@+id/dryer_source_about"
ndroid:layout_constraintStart_toStartOf="parent"
ndroid:layout_constraintTop_toBottomOf="@+id/textView3" />

<ImageView
android:id="@+id/dryer_source_about"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:layout_width="wrap_content"
ndroid:layout_constraintBottom_toBottomOf="@+id/dryer_heating_source_dropdown"
ndroid:layout_constraintEnd_toEndOf="parent"
ndroid:srcCompat="@android:drawable/ic_menu_info_details" />

<ImageView
android:id="@+id/qty_about"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_width="wrap_content"
ndroid:layout_constraintBottom_toBottomOf="@+id/washer_qty_layout"
ndroid:layout_constraintEnd_toEndOf="parent"
ndroid:srcCompat="@android:drawable/ic_menu_info_details" />

<!-- Washer Quantity -->
<android.support.design.widget.TextInputLayout
style="@style/TextInputLayout"
android:id="@+id/washer_qty_layout"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_width="0dp"
ndroid:layout_constraintEnd_toStartOf="@+id/dryer_source_about"
ndroid:layout_constraintStart_toStartOf="parent"
ndroid:layout_constraintTop_toBottomOf="@+id/dryer_heating_source_dropdown">

<android.support.design.widget.TextInputEditText
style="@style/EditTextViewBase"
android:hint="@string/washer_qty"
android:id="@+id/washer_qty"
android:inputType="number"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

</android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

显示了我的 calc_clothes_washer.xml 中的所有 View ,所有 View 都最小化在屏幕的左上角。非常感谢任何帮助或指导。

最佳答案

请检查下面的calc_clothes_washer.xml代码。它可能会解决你的问题。如果您遇到任何问题,请在评论部分告诉我。

使用您自己的绘图和资源。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/clothes_washer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background">

<!-- How is the water heated? -->
<TextView
android:id="@+id/heater_source_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="Water Heating Source"
android:textColor="@color/colorWhite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Spinner
android:id="@+id/water_heating_source_dropdown"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/water_heating_source_about"
app:layout_constraintStart_toStartOf="@+id/heater_source_label"
app:layout_constraintTop_toBottomOf="@+id/heater_source_label" />

<ImageView
android:id="@+id/water_heating_source_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/water_heating_source_dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_info_details" />

<!-- How is the Dryer Heated? -->
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="Dryer Heating Source"
android:textColor="@color/colorWhite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/water_heating_source_dropdown" />

<Spinner
android:id="@+id/dryer_heating_source_dropdown"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/dryer_source_about"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<ImageView
android:id="@+id/dryer_source_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="@+id/dryer_heating_source_dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_info_details" />

<ImageView
android:id="@+id/qty_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@+id/washer_qty_layout"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_info_details" />

<!-- Washer Quantity -->
<android.support.design.widget.TextInputLayout
android:id="@+id/washer_qty_layout"
style="@style/Widget.Design.TextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/dryer_source_about"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dryer_heating_source_dropdown">

<android.support.design.widget.TextInputEditText
android:id="@+id/washer_qty"
style="@style/EditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Washer Qty"
android:inputType="number" />

</android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

如果答案对您有用,请批准答案。谢谢!

关于java - 以编程方式在垂直 LinearLayout 内添加约束布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55842314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com