【问题标题】:How to put this layout in CardView?如何把这个布局放在 CardView 中?
【发布时间】:2017-04-28 18:26:52
【问题描述】:

所以我有一个布局,我想在 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>

【问题讨论】:

  • 你使用了很多可以直接用你的TextIputLayout替换的relativLayouts
  • @JonathanAste 对我没有帮助
  • 不是您正在寻找的解决方案,但这就是为什么我把它放在评论中,只是一个建议;)
  • @JonathanAste ;)

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


【解决方案1】:

嗯,首先,我想每个人都会同意,几乎每个RelativeLayout 都是无用的。他们实际上什么也没做,相信我。你可以删除它们,它会完全一样。

话虽如此,让我们进入正题。你希望这三个EditText 在Cardview 中,但CardView 只包含一个孩子。您必须将 CardView 与 LinearLayout 或 RelativeLayout 放在里面,然后在该布局中放置其他所有内容。

就像这样:

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多