首页 > 解决方案 > 有什么办法可以在按下一段时间后在另一个视图之上打开不同的视图

问题描述

我想实现类似 instagram 在这里所做的事情。长按帖子后,它会在屏幕顶部打开另一个视图以显示帖子并使屏幕的其余部分变得模糊。 在此处输入图像描述

标签: javaandroid

解决方案


要在 android 中创建类似的东西,您可以使用带有自定义布局的 alertDialog 并模糊背景: custom_layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:paddingLeft="30dp"
                  android:paddingRight="30dp"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <LinearLayout
                  android:orientation="horizontal"
                  android:paddingLeft="10dp"
                  android:paddingRight="10dp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
        <Button
            android:id="@+id/button1"
            android:layout_below
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        </LinearLayout>
     </LinearLayout>

活动中:

        //create an alert builder
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Name"); //you can add this to your custom layout only
        // set the custom layout
            final View view = getLayoutInflater().inflate(R.layout.custom_layout, null);
            builder.setView(view);
            Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
        // onclick functions
        // create and show the alert dialog
            AlertDialog dialog = builder.create();
            dialog.show();

对于模糊背景,您可以添加像https://stackoverflow.com/a/41373144/7364284这样的主题,但是我建议使用像https://stackoverflow.com/a/10381077/7364284这样的窗口属性,因为它的代码较少并且易于使用。


推荐阅读