如何将此布局放在CardView中?

时间:2017-04-28 18:26:53

标签: android android-layout android-linearlayout android-cardview android-relativelayout

所以我有一个布局,我想在1个CardView中显示前三个Inputlayouts / EditTexts ...下面是我的XML和一个看起来像是正确知道的图像。

我希望你能帮助我。

非常感谢你! :)

这是我的Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.MainActivity">

        <LinearLayout
            android:id="@+id/information"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:padding="16dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/field_education_year"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Ausbildungsjahr"
                        android:inputType="number"
                        android:maxLines="1" />

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

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="false">

                    <EditText
                        android:id="@+id/field_week_start"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusableInTouchMode="false"
                        android:hint="week start"
                        android:inputType="text|date"
                        android:onClick="onClick" />

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

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="false">

                    <EditText
                        android:id="@+id/field_week_end"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusableInTouchMode="false"
                        android:hint="week end"
                        android:inputType="text|date"
                        android:onClick="onClick" />

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

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/field_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Title"
                        android:maxLines="1" />

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

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/field_notice"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Notice"
                        android:inputType="textMultiLine"
                        android:scrollHorizontally="false" />

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

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_submit_post"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_margin="16dp"
                android:src="@drawable/ic_publish_new_report" />

        </LinearLayout>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

1 个答案:

答案 0 :(得分:0)

嗯,首先,我想每个人都会同意,那里几乎每个RelativeLayout都没用。他们一点都不做,相信我。你可以删除它们,它们将完全相同。

那就是说,让我们走到这一步。您希望EditText内有三个Cardview,但CardView只包含一个孩子。您必须将CardView放置在LinearLayoutRelativeLayout内,并在该布局中放置其他所有内容。

就像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/information"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".activities.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp">

                <EditText
                    android:id="@+id/field_education_year"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Ausbildungsjahr"
                    android:inputType="number"
                    android:maxLines="1" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:focusableInTouchMode="false">

                <EditText
                    android:id="@+id/field_week_start"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="false"
                    android:hint="week start"
                    android:inputType="text|date"
                    android:onClick="onClick" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:focusableInTouchMode="false">

                <EditText
                    android:id="@+id/field_week_end"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="false"
                    android:hint="week end"
                    android:inputType="text|date"
                    android:onClick="onClick" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp">

        <EditText
            android:id="@+id/field_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Title"
            android:maxLines="1" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp">

        <EditText
            android:id="@+id/field_notice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Notice"
            android:inputType="textMultiLine"
            android:scrollHorizontally="false" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_submit_post"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:src="@drawable/ic_publish_new_report" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>