在DialogFragment中创建完全自定义AlertDialog

时间:2013-05-20 13:15:35

标签: android android-alertdialog android-dialogfragment

下面是我的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();
    }
}

enter image description here

这是我的alertdialog layout xml fragment_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>

2 个答案:

答案 0 :(得分:3)

尝试使用它:

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;
    }

}

这将改变您的Dialog风格,并将删除默认背景。

答案 1 :(得分:0)

更改此行:

new AlertDialog.Builder(getActivity());

到此:

new AlertDialog.Builder(getActivity() , android.R.style.Theme_Translucent_NoTitleBar);