Basic usage of Android dialogfragment

Source: Internet
Author: User

Use of Android dialogfragment

The minimum API limit for Android systems is 11.

1. New Class inherited from dialogfragment

Click a button in the class to close the dialog box.

There is only one button in layout, and the code will not be pasted here.

Public class fragment_search extends dialogfragment {button btn_close; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate) ;}@ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {view v = Inflater. inflate (R. layout. search, container, false); // initialize the control btn_close = (button) v. findviewbyid (R. id. btn_cancel); btn_close.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {// close the dialog box dismiss () ;}}); Return V ;}


2. Use the fragment Activity

The mainactivity class must inherit fragmentactivity, because the following getsupportfragmentmanager must use

public class MainActivity extends FragmentActivity {public Button btn_search;Fragment_Search dialogFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_search = (Button) findViewById(R.id.btn_search);btn_search.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {showSearchDialog();}});}protected void showSearchDialog() {        // Create and show the dialog. if(dialogFragment == null)        dialogFragment = new Fragment_Search();dialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);        dialogFragment.show(getSupportFragmentManager(), "dialog");}}

You can use setstyle to change the dialogfragment style.


Summary:

The dialog box is displayed, but I do not know how to change the size of the displayed dialogfragment. If you have a method to change the size, please reply ......


I know a method to change the size. I don't know if it should be used in general, that is, to change the content size. It doesn't mean to change the padding value or height width of layout, instead, it changes the content padding. I changed the padding of a closed button, which was originally Android: layout_alignparentright = "true". After changing the padding, the height of the entire dialog becomes smaller, the default dialog is centered.

We look forward to a better way.


Contact Us

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

  • 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.