首页 > 解决方案 > 尝试在自定义布局中合并两个布局

问题描述

我正在尝试通过扩展线性布局将两个 xml 布局添加在一起,但我没有得到想要的结果。当我将此视图设置为 RecyclerView 时,我只会得到标题行

这里可能有什么问题?

我膨胀的两种 XML 布局都是线性布局类型,

class AddOnCardView : LinearLayout {

    private var itemData: AddOnCardData? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val header: LinearLayout = inflate(getContext(), R.layout.tw_dashboard_addon_header, this) as LinearLayout
        val childRow = inflate(getContext(), R.layout.tw_dashboard_addon_row, null)
        // prepareChildren(header)

        header.addView(childRow)
    }

    fun setDataItem(itemData: AddOnCardData) {
        this.itemData = itemData

    }


}

这是我的行的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/money_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="03dp"
            android:gravity="center|end"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="#FF0099FF"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="$88.00/mo" />

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="50dp"
            android:paddingEnd="05dp"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/money_text"
            tools:text="Unlimited Data" />

        <ccom.citrus.ui.widget.CustomTextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="50dp"
            android:paddingEnd="05dp"
            android:textColor="#FF9B9B9B"
            android:textSize="12sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label"
            tools:text="Get maximum 4G+ speeds" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="03dp"
            android:paddingEnd="10dp"
            app:layout_constraintBottom_toBottomOf="@+id/title"
            app:layout_constraintEnd_toEndOf="@+id/money_text" />


    </android.support.constraint.ConstraintLayout>

</LinearLayout>

这是我的标头的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <View
            android:id="@+id/divider_top"
            android:layout_width="match_parent"
            android:layout_height="05dp"
            android:background="@color/gray_light_redesign"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center|start"
            android:paddingStart="30dp"
            android:paddingEnd="30dp"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider_top"
            tools:text="ADD-ON-OPTIONS" />


        <!-- @drawable/ic_show_less-->
        <ImageView
            android:id="@+id/expand_collapse"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:paddingStart="05dp"
            android:paddingEnd="15dp"
            android:src="@drawable/ic_show_more"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="05dp"
            android:background="@color/gray_light_redesign"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/header_text" />


    </android.support.constraint.ConstraintLayout>
</LinearLayout>

标签: android

解决方案


看起来您没有将 AddOnCardView LinearLayout 设置为垂直方向,我认为它默认为水平方向......但由于您的标题宽度设置为match_parent,其他视图没有空间。

你可以通过调用setOrientation你的构造函数来做到这一点。


推荐阅读