对话框未显示

时间:2016-04-08 18:26:59

标签: android dialog

我编写了代码以显示自定义对话框。背景变得透明,如代码中所写,但对话框没有出现。

    protected void initiatedialog(View view) {

    final Dialog dialog = new Dialog(PostsActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.popup);

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = Math.round((width * 10) / 15);
    lp.height = Math.round(height / 3);
    dialog.getWindow().setAttributes(lp);
    final EditText etPostContent = (EditText) dialog.findViewById(R.id.etPostContent);
    Button bAddPost = (Button) dialog.findViewById(R.id.bAddPost);


    bAddPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //something
         dialog.dismiss();
        }
    });
    dialog.show();
}

以下是对话框自定义的xml文件。

popup.xml

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

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:gravity="center"
    android:background="@drawable/popup_background">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/etButton"
        android:hint="Write.."/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bAdd"
        android:layout_weight="7"
        android:text="Button"/>

1 个答案:

答案 0 :(得分:0)

好的,首先 - 它正在我的手机上运行(Android 4.0.3)。

由于我不知道你的一些变量/ drawable,我会稍微改变一下代码并描述一下差异。

1。 Popup.xml:

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

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/etPostContent"
        android:hint="Write.."/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bAddPost"
        android:layout_weight="7"
        android:text="Button"/>

</LinearLayout>

我不知道你的android:background="@drawable/popup_background" - 首先尝试删除此行并查看问题是否仍然存在 我也改变了视图的ID - 因为你在Java文件中指出了另一个名字。

2。 MainActivity(带导入)。

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initiatedialog(null);
    }


    protected void initiatedialog(View view) {

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.popup);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());

        int width = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        int height = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
        lp.width = Math.round((width * 10) / 15);
        lp.height = Math.round(height / 3);
        dialog.getWindow().setAttributes(lp);
        final EditText etPostContent = (EditText) dialog.findViewById(R.id.etPostContent);
        Button bAddPost = (Button) dialog.findViewById(R.id.bAddPost);

        bAddPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //something
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

有点评论 - 我不知道,你的宽度和高度是多少 - 所以我采取了窗口的宽度和高度;
请检查进口。

请看结果:
enter image description here

<强> PS。一点提示 - 您希望layout_weight xml属性正常工作 - 您应该设置layout_width = "0dp"。试着看看改变的结果。看到它:
enter image description here

请进行上述更改,看看发生了什么。感谢。

相关问题