Summary of support.v4.app.DialogFragment usage issues
Source: Internet
Author: User
<span id="Label3"></p><p><p>This dialog needs to be implemented due to app Requirements.</p></p><p><p></p></p><p><p><span style="line-height:1.5;">See here, you might say so easy things, and what else to say! So keep looking down!</span></p></p><p><p><strong>Scenario 1:</strong></p></p><pre class="brush:java; toolbar: true; auto-links: false;">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 (savedinstan cestate); Setcontentview (r.layout.activity_alert_dialog_fragment); Findviewbyid (r.id.show). setonclicklistener (new view.onclicklistener () {@Override public void Onclic K (View V) {showDialog (); } }); } public static class Myalertdialogfragment extends Dialogfragment {public static myalertdialogfragment Newinst ance (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 content area View Contentview = Layoutinflater.from (getactivity ()). Inflate (r.layout.basic_dialog_fragmen t, null); return new Alertdialog.builder (getactivity ()). SetIcon (r.drawable.ic_about). settitl E (title). setview (contentview). Setpositivebutton ("ok", New Dialoginterface.onclickl Istener () {@Override public void OnClick (dialoginterface dialog, int Which) {}}). Setnegativebutton ("cancel", New Dialoginterface.oncl Icklistener () {@Override Publicvoid OnClick (dialoginterface dialog, int which) {}}). creat E (); }} void ShowDialog () {dialogfragment newfragment = myalertdialogfragment.newinstance (r.string.two_buttons_t itle); Newfragment.show (getsupportfragmentmanager (), "dialog"); }}</pre><p><p><span style="line-height:1.5;">it actually Became.</span></p></p><p><p></p></p><p><p><span style="line-height:1.5;">the contents of the title area are not vertically centered, and the button area is completely aliased to the right,</span> <span style="line-height:1.5;">as shown, only the green content area is custom content (both the title and button styles are built into Android)</span></p></p><p><p><span>What is the cause of this? </span></p></p><p><p><span>this is because of the current reason for this alertdialogfragmentactivity Theme property, which inherits from Appcompatactivity requires <span style="color:#E53333;"> </span>the use of <span style="color:#E53333;">Theme.AppCompat.xxx </span>Series Theme</span></p></p><p><p>the <span style="line-height:1.5;">code (appcompatactivity,dialogfragment</span><span style="line-height:1.5;">, Alertdialog</span><span style="line-height:1.5;">) is the class in the support library, and these classes are used in You cannot use</span> the <span style="line-height:1.5;">dialogfragment</span> <span style="line-height:1.5;">and Alertdialog</span> <span style="line-height:1.5;"></span> in the support library directly in the activity</p></p><p><p>Try changing the Android:theme property of <span></span> appcompatactivity in Androidmanifest.xml to <span style="color:#E53333;">"@android: style/theme.holo" and so on <span style="color:#000000;">, Subsequent get one of the following exceptions</span> </span></p></p><p><p><span></span></p></p><p><p><span style="color:#E53333;">caused By:java.lang.IllegalStateException:You need to use a theme.appcompat Theme (or descendant) with this activity .</span></p></p><p><p><span></span></p></p><p><p>the <span style="color:#E53333;">Theme.AppCompat.xxx</span> Series Theme can be used for classes in Non-support library packages, which in turn <span style="color:#E53333;">Theme.Holo.xxx or <span style="color:#E53333;">Theme.Light.xxx</span> </span>series is not available for classes in the support library package</p></p><p><p>Try to <span> </span>Modify the Alertdialog.builder () code <span></span> in the Oncreatedialog of myalertdialogfragment</p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">New Alertdialog.builder (getactivity (), android. R.style.theme_holo_dialog)</pre></pre><p><p><span style="line-height:1.5;">get the following, but worse</span></p></p><p><p></p></p><p><p><strong>Scenario 2:</strong></p></p><pre class="brush:java; toolbar: true; auto-links: false;">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 Onclic K (View V) {showDialog (); } }); } public static class Myalertdialogfragment extends Dialogfragment {public static myalertdialogfragment Newinst ance (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 content area View Contentview = Layoutinflater.from (getactivity ()). Inflate (r.layout.basic_dialog_fragmen t, null); return new Alertdialog.builder (getactivity ()). SetIcon (r.drawable.ic_about). settitl E (title). setview (contentview). Setpositivebutton ("ok", New Dialoginterface.onclickl Istener () {@Override public void OnClick (dialoginterface dialog, int Which) {}}). Setnegativebutton ("cancel", New Dialoginterface.oncl Icklistener () {@Override public void OnClick (dialoginterface dialog, int WH Ich) { }}). Create (); }} void ShowDialog () {dialogfragment newfragment = myalertdialogfragment.newinstance (r.string.two_buttons_t itle); Newfragment.show (getfragmentmanager (), "dialog"); }}</pre><p><p><span style="line-height:1.5;">the basic ideals obtained are:</span></p></p><p><p></p></p><p><p>The Support library class is not used in scenario 2, and the <span style="color:#E53333;">Theme.AppCompat.xxx</span> <span style="color:#E53333;">series Theme</span> can be compatible with the Low-version api, but your app has a minimum API of 11 or More.</p></p><p><p>For these reasons, it is a good idea to customize the content area of the dialog (when you customize it, The impersonation defines both the title and the button in your layout), and then does not set the dialog Title,button property, only calls alertdialog.builder# Setview (view), or Dialog#setcontentview (view view) <span></span></p></p><p><p><br></p></p><p><p><br></p></p><p><p>Summary of support.v4.app.DialogFragment usage issues</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service