Android CardView水平中心

时间:2017-09-28 21:48:31

标签: android layout cardview

我已经阅读了几个关于如何水平对齐卡片视图的答案。我正在努力实现以下目标:

Desired Result

这是我编写的代码到目前为止(我已经从不同的帖子中找到了大约20个答案,没有一个对我有帮助。问题是左边和右边的边距或空格,我需要那个空间,但是卡片视图扩展了整个宽度。

<RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true">

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignStart="@+id/mProjectCDCNumCardView"
                android:layout_marginStart="7dp"
                android:layout_marginTop="7dp"
                android:text="DETAIL"
                android:textColor="#000"
                android:textSize="16sp" />

            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/mProjectCDCNumCardView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="13dp"
                card_view:cardCornerRadius="2dp"
                card_view:contentPaddingLeft="50dp"
                card_view:contentPaddingRight="@dimen/activity_horizontal_margin"
                card_view:contentPaddingTop="20dp"
                card_view:contentPaddingBottom="@dimen/activity_vertical_margin"
                android:background="@drawable/cardview_normal"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/textView8">

                    <EditText
                        android:id="@+id/mProjectCDCNumEditText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@drawable/editborder"
                        android:hint="Project Number"
                        android:inputType="textPersonName"
                        android:padding="10dp" />


            </android.support.v7.widget.CardView>

            <TextView
                android:id="@+id/mProjectAddressLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/editborder"
                android:layout_below="@id/mProjectCDCNumCardView"
                android:ems="16"
                android:padding="10dp"
                android:hint="Location"
                android:textColor="#000"
                android:textSize="18sp"
                android:drawableRight="@drawable/left_arrow"/>



        </RelativeLayout>

谢谢!

4 个答案:

答案 0 :(得分:0)

您只需将android:layout_marginStart和android:layout_marginEnd添加到您的cardview。

答案 1 :(得分:0)

布局的height属性必须是match_parent。

答案 2 :(得分:0)

在这里参加聚会的时间很少,但是我真的很喜欢使用 ConstraintLayouts 。将视图居中放置是非常容易的。

asyncData

此代码将 CardView 放在ConstraintLayout内部,该宽度与父级宽度相同。 CardView的宽度为200dp,由于其限制,它将在布局中水平居中。

More info about ConstraintLayout.

答案 3 :(得分:0)

  1. 在CardView组件中,添加一个 LinearLayout 组件。
  2. android:gravity="center_horizontal"属性添加到LinearLayout。
  3. 在LinearLayout组件中添加EditText组件。

    <android.support.v7.widget.CardView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp"
        app:cardCornerRadius="3dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal">
            <EditText
                    android:id="@+id/mProjectCDCNumEditText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/editborder"
                    android:hint="Project Number"
                    android:inputType="textPersonName"
                    android:padding="10dp" />
        </LinearLayout>