2013-05-20 74 views
3

以下是我的FragmentActivity和DialogFragment。我嘗試創建一個自定義AlertDialog。我已經部分實現了這一點,如下圖所示。我怎樣才能擺脫我的自定義AlertDialog周圍的白色區域?在DialogFragment中創建完全自定義的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佈局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> 

回答

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

} 

這將改變你的對話框的風格,將刪除默認背景。

+0

我已經嘗試過,但不會改變任何不幸的事情。我已將TAG更改爲「dialogg」,還是應將其保留爲TAG?它會拋出錯誤,因爲TAG不可見。我完全是新的 – mctuna

+0

你可以刪除'Log'。它只顯示'LogCat'中的消息。你能解釋你想從對話框中刪除哪部分? – hardartcore

+0

那些白色部分(背景)圍繞着我的黑色AlertDialog。 – mctuna

0

改變這一行:

new AlertDialog.Builder(getActivity()); 

這樣:

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

我已經把它完全放在這裏; builder.setView(view); .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); return builder.create(); 但引發錯誤,因爲「方法setBackgroundDrawable(ColorDrawable)未定義類型AlertDialog.Builder」 – mctuna

+0

模擬器無法運行該應用程序說不幸運行已停止。由於我沒有任何onCreateView,我已經覆蓋onCreateDialog。 – mctuna