【问题标题】:How do I create a popup overlay view in an activity without Fragment?如何在没有 Fragment 的活动中创建弹出叠加视图?
【发布时间】:2016-05-04 22:01:16
【问题描述】:

我想在按下按钮时在我的Activity 上显示一个弹出窗口。我受到了启发by this question

所以我在活动的内容 xml 中使用“合并”控件并将 2 个不同的布局放入其中,问题显然出现在这一行(代码取自上面链接的问题):

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
    .add(R.id.overlay_fragment_container, yourFragment)
    .commit();

因为当然,FragmentManager 适用于 Fragments

我的问题是我的Activity 没有“碎片化”。 LinearLayout 直接在Activity 中膨胀,而不是在Activity 内的Fragment 中。

我能否在Activity 中获得类似该问题的效果,或者我是否应该将其所有控件强制嵌入Fragment 中?

提前致谢

【问题讨论】:

  • 如果我的视图不应该只是一个多按钮询问选择,我是否也应该使用它?我需要一个带有 RatingBar 和 EditText 的评分视图来选择评论。 DialogFragment 对它有好处吗? (也许是的,因为我还要添加 2 个按钮来评价或取消)
  • 是的,AlertDialog 是用于确认等的标准对话框,但DialogFragment 更加灵活(您需要)。

标签: android android-layout android-fragments android-activity overlay


【解决方案1】:

好的,这可能看起来像很多代码,但它真的很容易。

首先,您将在 XML 中创建所需的对话框布局。 (与活动视图不在同一个 XML 中)这是一个示例。

custom_dialog.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:text="New Text"
        android:id="@+id/txtTitle" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_weight="1" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="52dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btnBtmLeft"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btnBtmRight"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

然后,在您的 Activity 中执行以下操作:

private void showMyDialog(Context context) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(true);

    TextView textView = (TextView) dialog.findViewById(R.id.txtTitle);
    ListView listView = (ListView) dialog.findViewById(R.id.listView);
    Button btnBtmLeft = (Button) dialog.findViewById(R.id.btnBtmLeft);
    Button btnBtmRight = (Button) dialog.findViewById(R.id.btnBtmRight);

    btnBtmLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    btnBtmRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do whatever you want here
        }
    });

    /**
     * if you want the dialog to be specific size, do the following
     * this will cover 85% of the screen (85% width and 85% height)
     */
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int dialogWidth = (int)(displayMetrics.widthPixels * 0.85);
    int dialogHeight = (int)(displayMetrics.heightPixels * 0.85);
    dialog.getWindow().setLayout(dialogWidth, dialogHeight);

    dialog.show();
}

最后,在您的活动的 onCreate 中,调用该方法

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showMyDialog(context);
    }
});

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多