android RelativeLayout how to get smaller size window

怎甘沉沦 提交于 2019-12-05 22:49:09

This will do what you want. Be aware that you'll get the full window height so dividing with 2 wont make it exactly half the size. I tried your layout dividing with 3 and it looks good.

public class DialogExampleActivity extends Activity {   

    private int mWindowHeight;

    private static final int DIALOG_OPEN = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Display display = getWindowManager().getDefaultDisplay();        
        mWindowHeight = display.getHeight();

        showDialog(DIALOG_OPEN);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog myDialog = new Dialog(DialogExampleActivity.this);

        switch (id) {
        case DIALOG_OPEN:
            myDialog.setContentView(R.layout.my_dialog);            
            myDialog.setCancelable(true);

            RelativeLayout rr = (RelativeLayout) myDialog.findViewById(R.id.relative_layout);
            LayoutParams params = rr.getLayoutParams();         
            params.height = mWindowHeight / 3;          
            rr.setLayoutParams(params);

            return myDialog;
        }

        return null;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!