解决安卓自定义Dialog不能水平填满屏幕

因为我发现dialog 默认的样式@android:style/Theme.Dialog 对应的style 有pading属性,

所以win.getDecorView().setPadding(0, 0, 0, 0); 就能够水平占满了


   ShareDialog shareDialog = new ShareDialog(HomeActivity.this);
        Window window = shareDialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        window.getDecorView().setPadding(0, 0, 0, 0);
        shareDialog.show();


非常感谢论坛http://bbs.csdn.net/topics/390175091

第14楼的解答


上面解决办法并没有真正解决,很多手机还是一样有问题

最新解决办法:

继承ShareDialog  extends AlertDialog

ShareDialog dlg = new ShareDialog(activity,shareBean);
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.gravity = Gravity.BOTTOM;
        dlg.show();


时间来到2015年5月14日,遗憾的是上面的解决办法是很多手机上是不适用的,到达怎么样才能水平填满呢,通过对微信DEMO的研究,发现

     final int cFullFillWidth = 10000;
                layout.setMinimumWidth(cFullFillWidth);

受此启发,我在我的布局文件里添加了这么一句:

android:minWidth="10000dp"

果然可以填充满了,泪流满面快哭了


下面给出我的完整写法:


弹出的:

    ShareDialog dlg = new ShareDialog(activity,shareBean);
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.gravity = Gravity.BOTTOM;
        dlg.show();


定义的:

    public ShareDialog(Activity mactivity,ShareBean shareBean) {
        super(mactivity,R.style.ShareDialog);
        this.mactivity = mactivity;
        this.shareBean =shareBean;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }


  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);
        shareGridView=(GridView)findViewById(R.id.share_gridView);
        cancelShare=(Button)findViewById(R.id.cancelShare);
        //分享
        shareItemlist = new ArrayList<ShareItem>();
        initData();
        shareAdapter=new ShareAdapter(getContext(), shareItemlist);
        shareGridView.setAdapter(shareAdapter);
        shareGridView.setOnItemClickListener(this);
        cancelShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareDialog.this.dismiss();
            }
        });
        mBundle=savedInstanceState;
    }


布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:minWidth="10000dp"//当水平布满的核心
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
    >
   <ImageView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#a7a7aa"
    android:id="@+id/splitShare"
    />

    <GridView
        android:id="@+id/share_gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="14dip"
        android:listSelector="@android:color/transparent"
        android:numColumns="3"
        android:scrollbars="vertical"
        android:stretchMode="columnWidth"
        android:verticalSpacing="14.0dip"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="26dp"
        />
    <Button
        android:id="@+id/cancelShare"
        style="@style/share_cancel_button"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_marginLeft="13dp"
        android:layout_marginRight="13dp"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="28dp"
        android:layout_height="43.5dp"
        android:text="@string/cancel_share"
        >
    </Button>
    </LinearLayout>


Dialog样式:(这个得写在style文件里,一开始我写在一个单独的theme文件中,导致bottom效果没了,一直在顶部,整体效果也很奇怪,原因不清楚

    <style name="ShareDialog"  parent="android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:windowBackground">@android:color/white</item>//必须还要再加上这一句才能看起来是满的
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>


简化版的微信DEMO:http://pan.baidu.com/s/1eQ4bJJc

你可能感兴趣的:(解决安卓自定义Dialog不能水平填满屏幕)