2019年10月10日星期四

Android Studio 如何在xml layout中应用Recyclerview

此文是下述链接的后续。
Android Studio 如何在xml layout中应用Cardview
注意的是Android Studio 3.5版本默认使用androidx。
在app level的build.gradle添加下文。

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <!-- A RecyclerView with some commonly used attributes -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout >


</RelativeLayout >

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>

2019年10月5日星期六

Android Studio 双击后退按钮退出程序

为了双击后退按钮退出安卓程序, 在MainActivity加入下列代码。
    long backKeyPressedTime;
    :
    :
    @Override //2秒内双击则退出程序
    public void onBackPressed() {
        //第一回按退出程序
        if(System.currentTimeMillis()>backKeyPressedTime+2000){
            backKeyPressedTime = System.currentTimeMillis();
        }
        //第二回按退出程序
        else{
            finish();
        }
    }

Android Studio 确认退出对话框

在MainActivity添加下列代码实现确认退出的对话框。

    @Override
    public void onBackPressed() {
        // AlertDialog 对话框
        AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
        alBuilder.setMessage("确定退出?");

        // 点击“是”选择退出
        alBuilder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        // 点击“否”返回
        alBuilder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        alBuilder.setTitle("结束程序");
        alBuilder.show(); // 显示 AlertDialog 对话框
    }