2019年10月6日星期日

Android Studio 如何在xml layout中应用Cardview

使用Cardview将使你的app显得特专业。
注意的是Android Studio 3.5版本默认使用androidx。
在app level的build.gradle添加下文。

dependencies {
    ......
    implementation 'androidx.cardview:cardview:1.0.0'
}

介绍xml之前先告诉你结果将如何呈现。

介绍的xml只是“一个”cardview的模型,只有结合了代码和recycler view才能像所看到的一样把结果排列成如图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:orientation="vertical">
    <!-- A CardView that contains a TextView -->
    <androidx.cardview.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="9dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <com.facebook.drawee.view.SimpleDraweeView
                    xmlns:fresco="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/ImageView_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitCenter"
                    android:background="@null"
                    android:layout_centerInParent="true"
                    fresco:placeholderImage="@mipmap/white_image"
                    />

                <TextView
                    android:id="@+id/TextView_title"
                    android:text="Title"
                    android:maxLines="3"
                    android:gravity="center"
                    android:layout_width="match_parent"
                    android:layout_height="90dp"
                    android:background="#A0000000"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:textSize="20sp"
                    android:ellipsize="end"
                    />
            </RelativeLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>
</LinearLayout>

没有评论:

发表评论