其他分享
首页 > 其他分享> > android-在DialogFragment中创建完全自定义AlertDialog

android-在DialogFragment中创建完全自定义AlertDialog

作者:互联网

以下是我的FragmentActivity和DialogFragment.我尝试创建一个自定义AlertDialog.我已经部分实现了,如下图所示.如何摆脱自定义AlertDialog周围的空白区域?

public class MainActivity extends FragmentActivity {    
public int mStackLevel = 0; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showDialog();    
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void showDialog(){
    mStackLevel++;
    FragmentTransaction ft = getSupportFragmentManager  ().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag   ("dialogg");
    if(prev != null)
    {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    DialogFragment df = AppWizard.newInstance(mStackLevel);
    df.show(ft, "dialogg");
}  
public void doPositiveClick() {
    // Do stuff here.
    showDialog();
}
public void doNegativeClick() {
    // Do stuff here. 
}
public static class AppWizard extends DialogFragment {
    int mNum;   
    static AppWizard newInstance(int num){
        AppWizard aw = new AppWizard();
        Bundle args = new Bundle();
        args.putInt("num", num);
        aw.setArguments(args);
        return aw;  
    }   
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");    
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("num");
        LayoutInflater lo = getActivity().getLayoutInflater();
        View view = lo.inflate(R.layout.fragment_dialog, null);   
        final CharSequence[] items = {"Bir daha gösterme!"};
        final boolean[] _selections = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());   
                builder.setView(view);            
                return builder.create();
    }
}

这是我的alertdialog布局xmlfragment_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_dialog"
    style="@style/AlertDialogCustom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    android:background="@android:drawable/alert_dark_frame" >

 <TextView 
     android:id="@+id/dialog_header"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="BLABLABLA"
     android:textColor="@android:color/black"
     />
  <TextView 
     android:id="@+id/dialog_header2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="BLABLABLA2"
     android:textColor="@android:color/black"
     />
  <Button
        android:id="@+id/dialog_button"
        android:layout_width="125dp"
        android:layout_height="50dp"
        android:text="Click"
        android:textColor="@android:color/background_dark"
        android:textSize="8sp"
        android:textStyle="bold|italic"
        />

</LinearLayout>

解决方法:

尝试使用这个:

public static class AppWizard extends DialogFragment {

    public static AppWizard newInstance(int num) {
        AppWizard f = new AppWizard();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, android.R.style.Theme_Light_Panel);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dialog,
                container, false);

        // declare your UI elements here
        // For example: 
        // Button mButton =  view.findViewById(R.id.dialog_button);
        // mButton.setOnClickListener(this);

        return view;
    }

}

这将更改对话框的样式,并删除默认背景.

标签:android-alertdialog,android-dialogfragment,android
来源: https://codeday.me/bug/20191123/2066374.html