Подтвердить что ты не робот

Как создать 100% пользовательский DialogFragment

Я хочу иметь диалоговое окно Android с использованием полностью настраиваемых фрагментов: ни одна из частей темы диалога платформы не включена. Например, что-то вроде этого:

Screen Shot

Как это сделать?

4b9b3361

Ответ 1

Следующий код Mono для Android С# делает трюк (но должен быть легко переноситься на Java). Я тестировал Android 2.2 (Galaxy S) и Android 4.1 (Nexus 7). Единственное, что вам нужно изменить, это идентификаторы макета, используемые для представления родительского элемента и диалогового окна.

[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 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 в https://github.com/t9mike/CustomDialogFragmentSample.

Ответ 2

Код ниже поможет вам отобразить полноэкранное диалоговое окно, а также установить прозрачный цвет

Dialog dialog = new Dialog(this);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// layout to display
dialog.setContentView(R.layout.about_program_dialog_layout);

// set color transpartent
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

dialog.show();

about_program_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#55000000" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="227dp"
        android:text="Dismiss" />

    <TextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="63dp"
        android:ems="10"
        android:text="Hello There World" />

</RelativeLayout>