Android 如何使CardView居中并获得适当的颜色背景?
我有一个应用程序,呈现如下: 白色矩形是一个Android 如何使CardView居中并获得适当的颜色背景?,android,layout,cardview,Android,Layout,Cardview,我有一个应用程序,呈现如下: 白色矩形是一个卡片视图。以下是呈现活动的.xml文件: <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/desi
卡片视图
。以下是呈现活动的.xml文件:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/designerGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
android:columnCount="5"
android:rowCount="6"
android:visibility="visible"
tools:context=".visuals.CloudCardActivity">
<!-- Left Control Panel (r:0 c:0 r-span:2 c-weight:2) -->
<LinearLayout
android:id="@+id/leftControlPanel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_row="0"
android:layout_rowSpan="6"
android:background="#0099cc"
android:gravity="top|center_horizontal"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:text="New" />
</LinearLayout>
<!-- Top Control Panel (r:0 c:1 r-span:1 c-span:3) -->
<LinearLayout
android:id="@+id/topControlPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_columnSpan="3"
android:layout_row="0"
android:background="#0099cc"
android:gravity="left|center_vertical"
android:orientation="horizontal">
<Spinner
android:id="@+id/cardFields"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="@array/default_card_fields">
</Spinner>
</LinearLayout>
<!-- Card View Panel (r:1 c:1 r-weight:8 c-weight:8) -->
<LinearLayout
android:id="@+id/cardLayout"
android:layout_column="2"
android:layout_columnSpan="3"
android:layout_row="1"
android:layout_rowSpan="5"
android:background="#a3a3a3"
android:elevation="3dp"
android:gravity="center_vertical|center_horizontal"
android:visibility="visible">
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="560dp"
android:layout_height="320dp"
android:background="@android:color/white"
android:fadingEdge="horizontal|vertical"
android:focusable="auto"
android:gravity="center_horizontal"
android:layoutMode="clipBounds"
app:cardElevation="5dp" />
</LinearLayout>
</GridLayout>
我是否对布局使用了正确的控件?试试这个
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="560dp"
android:layout_height="320dp"
android:background="@android:color/white"
android:fadingEdge="horizontal|vertical"
android:focusable="auto"
android:layout_gravity="center_horizontal"
android:layoutMode="clipBounds"
app:cardElevation="5dp" />
替代方法
CardView通常用于在ListView或RecyclerView中显示的项目列表中。
如果您只有一个矩形/卡片要显示,我建议使用具有圆角矩形背景的自定义视图对象
这可以通过以下方式实现
在可绘图文件夹“custom_card_bg.XML”中创建XML
接下来,在/layout文件夹中的自定义布局中说“custom\u rectangle\u layout.xml”,使用上面的自定义背景
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/custom_card_bg"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- your layout -->
</LinearLayout>
所以我要创建2.xml文件?一个在/可拉拔,另一个在/布局???无论如何,我可能需要一个自定义的视图。@IAbstract,是的,自定义视图需要2个xml布局。1个位于drawable中,用于定义您的背景;1个位于/layout文件夹中,用于将TextView等小部件添加到卡中。但请注意,我是如何使用drawable中的第一个xml设置背景的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/custom_card_bg"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- your layout -->
</LinearLayout>