support.v4.app.DialogFragment使用問題總結

來源:互聯網
上載者:User

標籤:

因app需求問題,需要實現這種dialog

看到這裡,你可能會說如此easy的事情,還說什麼! 那麼繼續往下看!

情境1:

import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.v4.app.DialogFragment;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.view.LayoutInflater;import android.view.View;public class AlertDialogFragmentActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_alert_dialog_fragment);        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                showDialog();            }        });    }    public static class MyAlertDialogFragment extends DialogFragment {        public static MyAlertDialogFragment newInstance(int title) {            MyAlertDialogFragment frag = new MyAlertDialogFragment();            Bundle args = new Bundle();            args.putInt("title", title);            frag.setArguments(args);            return frag;        }        @NonNull        @Override        public Dialog onCreateDialog(Bundle savedInstanceState) {            int title = getArguments().getInt("title");            // AlertDialog 的內容區            View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.basic_dialog_fragment, null);            return new AlertDialog.Builder(getActivity())                    .setIcon(R.drawable.ic_about)                    .setTitle(title)                    .setView(contentView)                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                        }                    })                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                        }                    })                    .create();        }    }    void showDialog() {        DialogFragment newFragment = MyAlertDialogFragment.newInstance(R.string.two_buttons_title);        newFragment.show(getSupportFragmentManager(), "dialog");    }}

實際成了這樣

title區的內容不是縱橫向置中的,button區完全走樣靠右對齊了,如所示,只有綠色內容區是自訂內容,(title和button樣式都是Android內建)

這是什麼原因造成的?

這是因為當前這個AlertDialogFragmentActivity的theme屬性的原因,它繼承自AppCompatActivity要求使用 Theme.AppCompat.xxx 系列theme

代碼中(AppCompatActivity,DialogFragment,AlertDialog)都是support library中的類,這些的類是配套使用的,你不能在Activity中直接使用support library中的DialogFragment和AlertDialog

嘗試在AndroidManifest.xml中將AppCompatActivity的android:theme屬性改成"@android:style/Theme.Holo"等,後續得到一個以下異常

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Theme.AppCompat.xxx 系列theme可以用於非support library包中的類,反過來Theme.Holo.xxx 或者 Theme.Light.xxx 等系列則無法用於support library包中的類

嘗試修改 MyAlertDialogFragment的onCreateDialog 中的AlertDialog.Builder()代碼

new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Dialog)

得到的如下,反而更差

情境2:

import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;import android.support.annotation.NonNull;import android.view.LayoutInflater;import android.view.View;public class AlertDialogFragmentActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_alert_dialog_fragment);        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                showDialog();            }        });    }    public static class MyAlertDialogFragment extends DialogFragment {        public static MyAlertDialogFragment newInstance(int title) {            MyAlertDialogFragment frag = new MyAlertDialogFragment();            Bundle args = new Bundle();            args.putInt("title", title);            frag.setArguments(args);            return frag;        }        @NonNull        @Override        public Dialog onCreateDialog(Bundle savedInstanceState) {            int title = getArguments().getInt("title");            // AlertDialog 的內容區            View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.basic_dialog_fragment, null);            return new AlertDialog.Builder(getActivity())                    .setIcon(R.drawable.ic_about)                    .setTitle(title)                    .setView(contentView)                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                        }                    })                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                        }                    })                    .create();        }    }    void showDialog() {        DialogFragment newFragment = MyAlertDialogFragment.newInstance(R.string.two_buttons_title);        newFragment.show(getFragmentManager(), "dialog");    }}

得到的基本理想:

情境2中沒有使用support library中類,而且Theme.AppCompat.xxx系列theme可以相容低版本API,但是你的App的最小API也要11以上。

鑒於以上原因,最好自訂Dialog的內容區(在你自訂時,模仿將title和button都定義到你的布局中),然後不設定Dialog的title,button屬性,只調用AlertDialog.Builder#setView(view),或者Dialog#setContentView(View view)



support.v4.app.DialogFragment使用問題總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.