Android CardView水平居中

问题描述 投票:0回答:5

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

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>

谢谢!

android layout cardview
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>

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

有关 ConstraintLayout 的更多信息。


1
投票
  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>
    


0
投票

您只需将 android:layout_marginStart 和 android:layout_marginEnd 添加到您的卡片视图中。


0
投票

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


0
投票

对我来说最简单的方法是在我的cardView中使用相对布局,然后使用“android:layout_centerHorizontal=”true”。 当相对布局的宽度和高度与父级匹配(父级是cardView)时,这非常有效。

© www.soinside.com 2019 - 2024. All rights reserved.