【问题标题】:Is there any way I can open a different view on top of another view upon pressing it for some time有什么办法可以在按下一段时间后在另一个视图之上打开不同的视图
【发布时间】:2020-06-07 12:56:37
【问题描述】:

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

【问题讨论】:

标签: java android


【解决方案1】:

要在 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 之类的窗口属性,因为它的代码更少且易于使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    相关资源
    最近更新 更多