如何创建一个100%的自定义DialogFragment [英] How to I create a 100% custom DialogFragment
问题描述
我想有使用片段是完全定制的一款Android对话框:没有一个平台对话的主题件均包括在内。举例来说,这样的事情:
I want to have an Android dialog using Fragments that is fully customized: none of the platform dialog theme pieces are included. For example, something like this:
我如何做到这一点?
推荐答案
对于Android C#code下单的伎俩(但应该是很容易地移植到Java)。我测试了在Android 2.2(银河S)和Android 4.1(的Nexus 7)。你需要修改的东西是用于父视图和对话视图布局的ID。
The following Mono for Android C# code does the trick (but should be easy to port to Java). I tested on Android 2.2 (Galaxy S) and Android 4.1 (Nexus 7). The only thing you would need to change are the layout IDs used for the parent view and dialog view.
[Activity (MainLauncher = true)]
public class TestCustomDialogActivity : FragmentActivity
{
public class MyDialogFragment : Android.Support.V4.App.DialogFragment
{
public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Android 3.x+ still wants to show title: disable
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
// CHANGE TO YOUR DIALOG LAYOUT or VIEW CREATION CODE
return inflater.Inflate(Resource.Layout.MyLayout, container, true);
}
public override void OnResume()
{
// Auto size the dialog based on it's contents
Dialog.Window.SetLayout(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
// Make sure there is no background behind our view
Dialog.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));
// Disable standard dialog styling/frame/theme: our custom view should create full UI
SetStyle(Android.Support.V4.App.DialogFragment.StyleNoFrame, Android.Resource.Style.Theme);
base.OnResume();
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// CHANGE TO YOUR MAIN SCREEN
SetContentView(Resource.Layout.MyDialog);
var dialog = new MyDialogFragment();
dialog.Show(SupportFragmentManager, "dialog");
}
}
我上传了完整的Mono为Android样品<一个href="https://github.com/t9mike/CustomDialogFragmentSample">https://github.com/t9mike/CustomDialogFragmentSample.
这篇关于如何创建一个100%的自定义DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!