安卓CardView如何水平居中

6

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

期望结果

这是我写的代码(我遵循了来自不同帖子的约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>

谢谢!

5个回答

2

虽然我来晚了,但我非常喜欢使用ConstraintLayouts。在其中居中视图非常容易。

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

     ... Content ...

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

</android.support.constraint.ConstraintLayout>

这段代码将会把宽度为200dp的CardView居中放置在与其父容器宽度相同的约束布局中。通过设置约束条件,它将在布局中水平居中。

更多关于约束布局的信息。


1
在您的CardView组件中,添加一个LinearLayout组件。 将android:gravity="center_horizontal"属性添加到您的LinearLayout中。 在您的LinearLayout组件中添加EditText组件。

0
你只需在你的CardView中添加android:layout_marginStart和android:layout_marginEnd属性即可。

0

您的布局的高度属性必须是 match_parent。


0
我发现最简单的方法是在我的cardView中使用相对布局,然后使用"android:layout_centerHorizontal="true""。 当相对布局的宽度和高度都设置为match parent(父布局为cardView)时,这个方法完美地起作用。

请不要在多个问题中发布相同的答案 - undefined
抱歉,我不知道,以为我在帮忙,因为同样的问题被问了很多次。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接