猿问

如何使用约束布局管理具有布局的自定义视图的宽度?

我有一个片段,我正在使用一个手风琴视图,我从GitHub下载了它 - https://github.com/riyagayasen/Android_accordion_view,在其主体(这是一个相对布局)中,我需要一个名为手风琴项目的自定义视图,就像图片上的第二个视图一样,我只是在片段的xml中制作:https://pp.userapi.com/c846123/v846123865/1d925d/CaEhr8uSLFA.jpg
但是由于某种原因,我的自定义视图(第一个)具有wrap_content宽度行为。

代码如下:

这是我的片段xml中手风琴视图的正文

<android.support.constraint.ConstraintLayout

                android:layout_width="match_parent"

                android:layout_height="wrap_content">


<!-- This is my custom view -->

<!-- Notice that the width is set to match_constraint -->


                <com.app.myapp.views.AccordionItem

                    android:id="@+id/item"

                    android:layout_width="0dp"

                    android:layout_height="wrap_content"

                    android:layout_marginStart="8dp"

                    app:isStarred="true"

                    app:layout_constraintEnd_toEndOf="parent"

                    app:layout_constraintStart_toStartOf="parent"

                    app:layout_constraintTop_toTopOf="parent"

                    app:title="Another title" />


<!-- This is an example of how my view should look like -->


                <android.support.constraint.ConstraintLayout

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:layout_marginStart="8dp"

                    android:layout_marginTop="8dp"

                    android:background="@drawable/accordion_item"

                    app:layout_constraintStart_toStartOf="parent"

                    app:layout_constraintTop_toBottomOf="@+id/item">


                </android.support.constraint.ConstraintLayout>

            </android.support.constraint.ConstraintLayout>


开满天机
浏览 112回答 2
2回答

呼唤远方

没有父级的膨胀方法将丢失布局参数(高度,宽度)private void initView() {&nbsp; &nbsp; inflate(getContext(), R.layout.accordion_item, this);&nbsp; &nbsp; // or&nbsp; &nbsp; // View view = LayoutInflater.from(getContext()).inflate(R.layout.accordion_item, this, false);&nbsp; &nbsp; // addView(view);}

喵喵时光机

你真的不需要这个简单布局的库,看看这个例子:<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"tools:context=".Fragments.CheckoutScreen"><!-- This is an example of how my view should look like --><CheckBox&nbsp; &nbsp; android:id="@+id/favourite_checkBox"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginEnd="8dp"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="@+id/textView4"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@+id/textView4" /><TextView&nbsp; &nbsp; android:id="@+id/textView5"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; android:text="T"&nbsp; &nbsp; android:textSize="18sp"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="@+id/textView4"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@+id/textView4" /><TextView&nbsp; &nbsp; android:id="@+id/textView4"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; android:text="Title"&nbsp; &nbsp; android:textSize="18sp"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/textView5"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; app:layout_constraintVertical_bias="0.383" />
随时随地看视频慕课网APP

相关分类

Java
我要回答