【问题标题】:Android CardView center horizontallyAndroid CardView 水平居中
【发布时间】:2018-03-10 18:27:21
【问题描述】:

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

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


    【解决方案1】:

    在这里聚会有点晚了,但我真的很喜欢使用 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,由于受到约束,它将在布局中水平居中。

    More info about ConstraintLayout.

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        你的布局的 height 属性必须是 match_parent。

        【讨论】:

          【解决方案4】:
          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>
            

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-28
            • 2012-09-04
            • 2013-03-30
            • 2015-01-14
            • 2011-01-17
            相关资源
            最近更新 更多