我想创建卡片 -
我尝试使用 margin
,但没有用这个想法取胜。我尝试为所有元素创建个人 View 并使用 padding
或 margin
但我没有结果。
我有这样的代码
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:maxLines="2"
android:textStyle="bold"
android:text="title"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:maxLines="5"
android:text="content" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/content"
android:layout_marginBottom="8dp"
android:background="@null"
android:src="@drawable/ic_more_vert_black_36dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
感谢您的关注。
最佳答案
Android 已经为卡片和回收站 View 预定义了小部件。您可以使用 android cardview 和 recycler view 来解决您的问题,它很容易实现。
为此 如果你使用 android studio 作为 IDE,你首先添加依赖项
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
然后在上面提到的代码中将其更改为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
>
<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:maxLines="2"
android:textStyle="bold"
android:text="title"/>
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="12dp"
android:maxLines="5"
android:text="content" />
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/description"
android:layout_marginTop="16dp"
android:maxLines="5"
android:text="content" />
<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 1"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 2"
android:id="@+id/button2"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是教程:link
关于android - android中的边距和布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36239648/