具有固定宽高比的FragmentDialog

时间:2017-01-09 09:14:12

标签: android android-dialogfragment

我尝试使用固定宽高比实现FragmentDialog。它应该与屏幕高度一样高(减去一些填充),宽度应为dialogWidth=dialogHeight*ar。自FragmentDialog以来没有提供onMeasure方法。我在对话框的内容类中重写了这个方法:

public class PuzzleSelectorView extends FrameLayout {
public PuzzleSelectorView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    //Log.d("selector",heightSize+"");
    int dy=((ImageView)this.findViewById(R.id.selector_start_btn)).getHeight();
    int desiredWidth=(int) ((heightSize-dy) *ImagesItemView.IMAGE_ASPECT_RATIO);
    widthMeasureSpec=MeasureSpec.makeMeasureSpec(desiredWidth,MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    this.setMeasuredDimension(desiredWidth, heightSize);
    //this.setMeasuredDimension(320,240);
}
}

片段类:

public class PuzzleSelectorFragment extends DialogFragment {
private Decryptor loader;
public void show(FragmentManager manager,String tag, String imgId) {
    super.show(manager,tag);
    //some unrelated code
}
@Override
public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);
    Log.d("dialog","closed");
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_image_selector,null);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

}
@Override
public void onStart()
{
    super.onStart();
}
}

内容布局:

<?xml version="1.0" encoding="utf-8"?>
<com.rhyboo.net.listtest.layouts.PuzzleSelectorView  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/selector_img_container"
    android:layout_marginBottom="48dp"
    android:scaleType="fitXY" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:srcCompat="@color/colorAccent"
    android:id="@+id/selector_start_btn"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_gravity="bottom" />

<com.rhyboo.net.listtest.layouts.CustomTextView
    app:customFont="Lato-Regular.ttf"
    android:text="@string/start_puzzle_btn"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:id="@+id/textView"
    android:layout_gravity="bottom|center_horizontal"
    android:gravity="center_vertical"
    android:textSize="18sp"
    android:textColor="@color/background" />

<ProgressBar
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/selector_preloader"
    android:layout_gravity="center_vertical|center_horizontal" />

要表明我的意思:

PuzzleSelectorFragment selector=new PuzzleSelectorFragment();                    
selector.show(MainActivity.this.getSupportFragmentManager(),"selector",id);

它在真实设备上运行良好(Android 5.1.1): real device

但是当我在模拟器(Android 7.1)上运行时,我的布局已损坏: enter image description here

我的问题是:我是否通过在内容类onMeasure中设置片段的大小来朝着正确的方向前进?如果不是我应该在哪里设置它?

1 个答案:

答案 0 :(得分:0)

出于某种原因,如果我使用setMeasuredDimension设置内容的视图大小,则android 7中的FragmentDialog无法正确布局ImageViews,其中包含BitmapDrawable。通过覆盖onLayout进行解决并使用所需尺寸调用孩子的layout方法。