2015-01-14 4 views
0

Я пытаюсь создать диалоговое фрагмент, который занимает 98% ширины и 90% процентов высотыAndroid Диалог изменить размер фрагмента

@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' в обоих случаях диалог появляется без изменений даже при попытке ввести жестко кодированную ширину и высоту.
Как это сделать?

+0

Не по теме: Вы хотите использовать 'dialog.getLayoutInflater (...)' раздувать внутренности диалога. Представьте, что у вас была темная активность и белый диалог. –

+0

Я пробовал, не работал – NickF

ответ

0

Override размер окна, как это:

@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(); 
} 
Смежные вопросы