简体   繁体   English

显示 Android Wear 风格的 AlertDialog

[英]Showing Android Wear style AlertDialog

I'm looking for a way to recreate the alert dialog in the Setting application of Android Wear:我正在寻找一种在 Android Wear 的设置应用程序中重新创建警报对话框的方法:

预期的

Which is swipe to dismissable.这是滑动到可解雇。

But instead, what I got is this:但相反,我得到的是:

实际的

Just a barebone Android dialog.只是一个准系统的 Android 对话框。 How can I show the AlertDialog in the Settings.apk style?如何在 Settings.apk 样式中显示 AlertDialog? (Which I think must be default for Android Wear application) (我认为必须是 Android Wear 应用程序的默认设置)

I found no default way to do this, also setting a custom view to an AlertDialog did not look good.我发现没有默认的方法可以做到这一点,还将自定义视图设置为 AlertDialog 看起来不太好。 You can still try though, maybe a different Theme works.你仍然可以尝试,也许不同的主题有效。

What I did was create a new Activity and create my own layout which looks like this:我所做的是创建一个新的Activity并创建我自己的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp"
        app:layout_box="all">

        <TextView
            android:id="@+id/tv_longtext"
            android:layout_width="match_parent"
            android:layout_height="0sp"
            android:layout_weight="1"
            android:fontFamily="sans-serif-condensed"
            android:gravity="bottom"
            android:padding="5sp"
            android:text="Ambient screen reduces battery life."
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-condensed"
            android:gravity="center_horizontal|top"
            android:paddingBottom="15sp"
            android:paddingTop="5sp"
            android:text="Turn on?"
            android:textSize="18sp" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5sp">

            <android.support.wearable.view.CircledImageView
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:src="@drawable/ic_cross"
                app:circle_color="#AFAFAF"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />

            <android.support.wearable.view.CircledImageView
                android:id="@+id/btn_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:src="@drawable/ic_tick"
                app:circle_color="#0EB695"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />
        </FrameLayout>
    </LinearLayout>
</android.support.wearable.view.BoxInsetLayout>

It looks just like the confirmation screen from the settings.它看起来就像设置中的确认屏幕。 Maybe it still needs some tweaks, but I think this is the way to go.也许它仍然需要一些调整,但我认为这是要走的路。

I had a similar problem and indeed I didn't find a default way to do this.我有一个类似的问题,确实我没有找到一个默认的方法来做到这一点。 I tried to use AlertDialogs for WearOs and they don't look well, because even if you pass them a custom view, the AlertDialog class crops the layout in some unexpected ways.我尝试将 AlertDialogs 用于 WearO,但它们看起来不太好,因为即使您将自定义视图传递给它们,AlertDialog 类也会以一些意想不到的方式裁剪布局。 How I ended up solving the problem is using the Dialog class (AlertDialog's parent class) and passing it a custom view.我最终解决问题的方法是使用 Dialog 类(AlertDialog 的父类)并将其传递给自定义视图。 The Dialog class doesn't alter the layout and you can attach the dialog to an activity's lifespan (which is the idea of dialogs, creating a custom activity doesn't fit with this requirement). Dialog 类不会改变布局,您可以将对话框附加到活动的生命周期(这是对话框的想法,创建自定义活动不符合此要求)。

So you could create a function like this inside your activity:所以你可以在你的活动中创建一个这样的函数:

private void showDialog() {
  Dialog dialog = new Dialog(this);
  View myLayout = getLayoutInflater().inflate(R.layout.my_layout_id, null);
  Button positiveButton = myLayout.findViewById(R.id.positive_button);
  positiveButton.setOnClickListener(
    v -> {
      /* Your action on positive button clicked. */
    }
  );

  Button negativeButton = myLayout.findViewById(R.id.negative_button);
  negativeButton.setOnClickListener(
    v -> {
      /* Your action on negative button clicked. */
    }
  );

  dialog.setContentView(myLayout);
  dialog.show();
}

I created a similar fragment with custom layout like this:我创建了一个类似的具有自定义布局的片段,如下所示: 在此处输入图片说明

Kotlin code:科特林代码:

/**
 * Created by nmbinh87@gmail.com on 4/12/21.
 */
class ConfirmationDialog private constructor() : DialogFragment(R.layout.confirmation_dialog) {
    var listener: Listener? = null
    var longMessage: String = ""
    var shortMessage = ""
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_longtext.text = longMessage
        tv_question.text = shortMessage
        tv_longtext.visibility = if (longMessage.isEmpty()) View.GONE else View.VISIBLE
        tv_question.visibility = if (shortMessage.isEmpty()) View.GONE else View.VISIBLE
        btn_cancel.setSafeOnClickListener {
            dismiss()
            listener?.onCancel()
        }
        btn_ok.setSafeOnClickListener {
            dismiss()
            listener?.onConfirm()
        }
    }

    override fun onStart() {
        super.onStart()
        val params = dialog?.window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.MATCH_PARENT
        dialog?.window?.attributes = params as WindowManager.LayoutParams
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
    }

    open class Listener {
        fun onCancel() {
        }

        open fun onConfirm() {
        }
    }

    companion object {
        fun show(
            fm: FragmentManager,
            longMessage: String = "",
            shortMessage: String = "",
            listener: Listener
        ) {
            val fragment = ConfirmationDialog()
            fragment.longMessage = longMessage
            fragment.shortMessage = shortMessage
            fragment.listener = listener
            fragment.show(fm, fm::class.simpleName)
        }
    }
}

Layout:布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout 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:layout_width="match_parent"
    android:background="@color/colorPrimaryDark"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp"
        app:layout_box="all">

        <TextView
            android:id="@+id/tv_longtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:fontFamily="sans-serif-condensed"
            android:gravity="center"
            android:padding="5sp"
            android:textColor="@color/white"
            android:textSize="16sp"
            tools:text="Ambient screen reduces battery life." />

        <TextView
            android:id="@+id/tv_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-condensed"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingTop="5sp"
            android:paddingBottom="15sp"
            android:textColor="@color/white"
            android:textSize="18sp"
            tools:text="Turn on?" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5sp">

            <android.support.wearable.view.CircledImageView
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:src="@drawable/ic_cc_clear"
                app:circle_color="#AFAFAF"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />

            <android.support.wearable.view.CircledImageView
                android:id="@+id/btn_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:src="@drawable/ic_cc_checkmark"
                app:circle_color="#0EB695"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />
        </FrameLayout>
    </LinearLayout>
</android.support.wearable.view.BoxInsetLayout>

Usage:用法:

   ConfirmationDialog.show(
        childFragmentManager,
        "",
        "Turn alarm off?",
        object : ConfirmationDialog.Listener() {
            override fun onConfirm() {
                super.onConfirm()
                turnAlarm(false)
            }
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM