appcompatDialogFragment与appcompat-v7:23.1.0有关

时间:2015-10-19 18:14:44

标签: android android-appcompat appcompatdialogfragment appcompat-v7-r23.1

我将appcompat v7从版本 23.0.1 升级到版本 23.1.0

我发现了AppCompatDialogFragment的惊喜:在对话框的顶部显示了一个额外的空格

其他人正在经历这个?

enter image description here

这是我的代码:

public class AlertDialogFragment extends AppCompatDialogFragment {

    public static final int ALERTDIALOG_STARTUP = 101;
    public static final int ALERTDIALOG_DELETE_SEARCH_HISTORY = 102;
    public static final int ALERTDIALOG_RATE_APP = 103;
    public static final int ALERTDIALOG_REACHED_FOLLOWS_FREE_LIMIT = 104;
    public static final int ALERTDIALOG_UNFOLLOW_ROUTE = 105;
    AlertDialogListener mListener;

    public static AlertDialogFragment newInstance(int type, String id, String[] params) {
        AlertDialogFragment frag = new AlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putString("id", id);
        args.putStringArray("params", params);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        try {
            mListener = (AlertDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement AlertDialogListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.view_alertdialog, container, false);
        int type = getArguments().getInt("type");
        String[] params = getArguments().getStringArray("params");
        switch (type) {
            case ALERTDIALOG_STARTUP:
                setCancelable(false);
                view = setupView(view, getString(R.string.startup_title), getString(R.string.startup_message), getString(R.string.go_to_facebook), getString(R.string.go_to_app));
                break;
            case ALERTDIALOG_DELETE_SEARCH_HISTORY:
                view = setupView(view, params[0], getString(R.string.remove_search_message), getString(android.R.string.yes), getString(android.R.string.no));
                break;
            case ALERTDIALOG_RATE_APP:
                setCancelable(false);
                view = setupView(view, getString(R.string.rate_app_title), getString(R.string.rate_app_message), getString(R.string.rate_app_yes), getString(R.string.rate_app_no));
                break;
            case ALERTDIALOG_REACHED_FOLLOWS_FREE_LIMIT:
                view = setupView(view, params[0], params[1], params[2], null);
                break;
            case ALERTDIALOG_UNFOLLOW_ROUTE:
                view = setupView(view, params[0], getString(R.string.unfollow_route_message), getString(android.R.string.yes), getString(android.R.string.no));
                break;
        }
        return view;
    }

    private View setupView(View view, String title, String text, String positiveButtonLabel, String negativeButtonLabel) {
        TextView customTitle = (TextView) view.findViewById(R.id.custom_title);
        customTitle.setText(title);
        TextView customMessage = (TextView) view.findViewById(R.id.custom_message);
        customMessage.setText(Html.fromHtml(text));

        if (positiveButtonLabel!=null) {
            Button positiveButton = (Button) view.findViewById(R.id.positive_button);
            positiveButton.setVisibility(View.VISIBLE);
            positiveButton.setText(positiveButtonLabel);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mListener.onAlertDialogPositiveClick(getArguments().getInt("type"), getArguments().getString("id"));
                    dismiss();
                }
            });
        }

        if (negativeButtonLabel!=null) {
            Button negativeButton = (Button) view.findViewById(R.id.negative_button);
            negativeButton.setVisibility(View.VISIBLE);
            negativeButton.setText(negativeButtonLabel);
            negativeButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mListener.onAlertDialogNegativeClick(getArguments().getInt("type"), getArguments().getString("id"));
                    dismiss();
                }
            });
        }
        return view;
    }

    public interface AlertDialogListener {
        public void onAlertDialogPositiveClick(int dialogType, String id);
        public void onAlertDialogNegativeClick(int dialogType, String id);
    }
}

这是view_alertdialog.xml布局文件:

<LinearLayout
    android:orientation="vertical"
    android:paddingTop="@dimen/alert_dialog_padding_top"
    android:paddingLeft="@dimen/alert_dialog_padding_hor"
    android:paddingRight="@dimen/alert_dialog_padding_hor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/custom_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/evidence_color"
        android:textStyle="bold"
        android:textSize="@dimen/alert_dialog_title_textsize" />

    <TextView
        android:id="@+id/custom_message"
        android:layout_marginTop="@dimen/about_activity_paragraph_marginTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/top_color"
        android:textSize="@dimen/alert_dialog_message_textsize" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        style="?attr/buttonBarStyle">

        <Button
            android:id="@+id/negative_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/cancel"
            android:textStyle="bold"
            android:textColor="@color/link_color"
            android:visibility="gone"
            style="?attr/buttonBarButtonStyle"/>

        <Button
            android:id="@+id/positive_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/ok"
            android:textStyle="bold"
            android:textColor="@color/link_color"
            android:visibility="gone"
            style="?attr/buttonBarButtonStyle"/>

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:19)

这是AppCompatDialogFragment的标题。您可以使用以下代码隐藏标题:

public class AlertDialogFragment extends AppCompatDialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }
}