亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用約束布局管理具有布局的自定義視圖的寬度?

如何使用約束布局管理具有布局的自定義視圖的寬度?

開滿天機 2022-09-14 10:27:31
我有一個片段,我正在使用一個手風琴視圖,我從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>
查看完整描述

2 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

沒有父級的膨脹方法將丟失布局參數(高度,寬度)


private void initView() {

    inflate(getContext(), R.layout.accordion_item, this);

    // or

    // View view = LayoutInflater.from(getContext()).inflate(R.layout.accordion_item, this, false);

    // addView(view);

}


查看完整回答
反對 回復 2022-09-14
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

你真的不需要這個簡單布局的庫,看看這個例子:


<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

    android:id="@+id/favourite_checkBox"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginEnd="8dp"

    app:layout_constraintBottom_toBottomOf="@+id/textView4"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintTop_toTopOf="@+id/textView4" />


<TextView

    android:id="@+id/textView5"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginStart="8dp"

    android:text="T"

    android:textSize="18sp"

    app:layout_constraintBottom_toBottomOf="@+id/textView4"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="@+id/textView4" />


<TextView

    android:id="@+id/textView4"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginStart="8dp"

    android:text="Title"

    android:textSize="18sp"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintStart_toEndOf="@+id/textView5"

    app:layout_constraintTop_toTopOf="parent"

    app:layout_constraintVertical_bias="0.383" />


查看完整回答
反對 回復 2022-09-14
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號