Android Dialog片段更改大小

时间:2015-01-14 08:41:01

标签: android android-dialogfragment

我正在尝试创建一个占用98%宽度和90%高度

的对话框片段
   @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        int width = (int)(0.98 * getResources().getDisplayMetrics().widthPixels);
        int height = (int)(0.9 * getResources().getDisplayMetrics().heightPixels);

        dialog.getWindow().setLayout(width, height);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        View root = getActivity().getLayoutInflater().inflate(R.layout.dialog_collector_rating, null);
        getReference(root);
        dialog.setContentView(root);

        return dialog;
    }

    private void getReference(View root){

        if(mRates == null){
            mRates = new ArrayList<TopRateDataItem>();
        }

        // list
        ListView ratingList = (ListView) root.findViewById(R.id.ratingList);
        RatingAdapter adapter = new RatingAdapter();
        ratingList.setAdapter(adapter);

        // workers number
        TextView tvWorkersNum = (TextView) root.findViewById(R.id.tvWorkersNum);
        tvWorkersNum.setText(getString(R.string.dialog_collector_rating_workers, mRates.size()));

        // button
        View btnClose = root.findViewById(R.id.btnClose);
        btnClose.setOnClickListener(this);
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//
//        if(mRates == null){
//            mRates = new ArrayList<TopRateDataItem>();
//        }
//
//        // Inflate view
//        View root = getActivity().getLayoutInflater().inflate(R.layout.dialog_collector_rating, null);
//
//        // list
//        ListView ratingList = (ListView) root.findViewById(R.id.ratingList);
//        RatingAdapter adapter = new RatingAdapter();
//        ratingList.setAdapter(adapter);
//
//        // workers number
//        TextView tvWorkersNum = (TextView) root.findViewById(R.id.tvWorkersNum);
//        tvWorkersNum.setText(getString(R.string.dialog_collector_rating_workers, mRates.size()));
//
//        // button
//        View btnClose = root.findViewById(R.id.btnClose);
//        btnClose.setOnClickListener(this);
//
//        return root;
//    }

布局:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:background="@color/dialog_title_background"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/dialog_title_text_color"
        android:text="@string/dialog_collector_rating_title"/>

    <ListView
        android:id="@+id/ratingList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <Button
            android:id="@+id/btnClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="25dp"
            android:paddingLeft="25dp"
            android:background="@drawable/button_grey_selector"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:text="@string/dialog_collector_rating_button_text"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="right|center_vertical">

            <TextView
                android:id="@+id/tvWorkersNum"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:maxLines="1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@string/dialog_collector_rating_all"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

我尝试使用onCreateDialog和'onCreateView',在这两种情况下,即使尝试输入硬编码的宽度和高度,对话框也不会发生任何变化。
如何做到这一点?

1 个答案:

答案 0 :(得分:0)

覆盖窗口的大小,如下所示:

@Override
public void onResume() {

    // Store access variables for window and blank point
    Window window = getDialog().getWindow();
    Point size = new Point();

    // Store dimensions of the screen in `size`
    Display display = window.getWindowManager().getDefaultDisplay();
    display.getSize(size);

    //resize
    window.setLayout((int) (size.x *0.98), (int) (size.y *0.9));
    window.setBackgroundDrawableResource(android.R.color.transparent);
    window.setGravity(Gravity.CENTER);

    super.onResume();
}