2013-03-02 86 views
10

我試圖用自定義view(無標題或頁腳)和圓角構建AlertDialog。我見過很多關於如何做的文章,我嘗試了很多東西,但是我無法像我想的那樣構建它。Android:具有自定義視圖和圓角的AlertDialog

這是我的目標:

enter image description here

我創建了一個drawabledialog稱爲dialog_background.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <solid 
     android:color="#FFAAAAAA" /> 

    <stroke 
     android:width="2dp" 
     android:color="#FF000000" /> 

    <corners android:radius="20dp" /> 
</shape> 

而且我添加了一個style使用它:

<style name="MyDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:background">@drawable/dialog_background</item> 
</style> 

我的自定義viewlayout會有兩個按鈕。現在我向你展示一個空的LinearLayout,以簡化它。這是playdialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    style="@style/MyDialog" 
    > 

</LinearLayout> 

要構建Dialog我使用DialogFragment。這是它的onCreateDialog功能:

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 
    return builder.create(); 
} 

好吧,如果我用這樣的代碼,我得到這樣的:

enter image description here

我試圖設置dialog背景透明修改我DialogFragment代碼:

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 

    **NEW**   

    Dialog d = builder.create(); 
    d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    return d; 
} 

結果是完全一樣的,然後我意識到那個白色的矩形u nder my dialog來自我的自定義view,而不是從dialog。我已經將我的view的背景設置爲dialog_background.xml,因此我無法將其設置爲透明,或者我鬆散了角落,顏色等。

然後我決定修改dialog的背景dialog_background.xml並讓我的視圖具有純色作爲背景。

在我的自定義viewlayout(playdialog.xml)我換成風格= 「@風格/ MyDialog」 有:

android:background="#FFAAAAAA" 

然後在我DialogFragment我用這個代碼:

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 

    **NEW**   

    Dialog d = builder.create(); 
    d.getWindow().setBackgroundDrawableResource(R.drawable.dialog_background); 
    return d; 
} 

這就是我得到的:

enter image description here

這幾乎是我想要的,但你可以看到我的自定義view邊框,所以它不夠好。在這一點上,我不知道我還能做什麼。

任何人都知道我該如何解決它?

謝謝!

+0

你能解決這個問題嗎?使用圓角時,我無法消除波紋管中的白色部分 – sabbir 2015-01-07 20:32:52

回答

5

我剛剛創建一個自定義警告對話框。但其角落不圓。

首先爲它創建一個佈局 -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="240sp" 
android:layout_height="wrap_content" 
android:background="#FFFFFF" 
tools:ignore="SelectableText" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1sp" 
    tools:ignore="UselessParent" > 

    <TableLayout 
     android:id="@+id/tablelayout_dialog_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:stretchColumns="1" > 

     <TableRow 
      android:id="@+id/tablerow_dialog_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      tools:ignore="UselessParent" > 

      <ImageView 
       android:id="@+id/imageview_dialog_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/alertwhite" 
       android:layout_gravity="center" 
       android:background="#643c3a" 
       android:contentDescription="@string/string_todo"/> 

      <TextView 
       android:id="@+id/textview_dialog_title" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:background="#643c3a" 
       android:padding="10sp" 
       android:gravity="center_vertical" 
       android:textColor="#FFFFFF" 
       android:textSize="15sp" /> 

     </TableRow>  
    </TableLayout> 

    <View 
     android:id="@+id/viewline_dialog" 
     android:layout_below="@+id/tablelayout_dialog_title" 
     android:layout_width = "wrap_content" 
     android:layout_height="0.25dip" 
     android:background="#ffffff"   
     android:layout_centerVertical ="true" /> 

    <TextView 
     android:id="@+id/textview_dialog_text" 
     android:layout_below="@+id/viewline_dialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="8sp" 
     android:background="#643c3a" 
     android:textColor="#FFFFFF" 
     android:textSize="12sp" /> 

    <View 
     android:id="@+id/viewline1_dialog" 
     android:layout_width = "wrap_content" 
     android:layout_height="0.5dip" 
     android:background="#ffffff"   
     android:layout_centerVertical ="true" 
     android:layout_below="@+id/textview_dialog_text"/> 

    <TableLayout 
     android:id="@+id/tablelayout_dialog_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:stretchColumns="*" 
     android:layout_below="@+id/viewline1_dialog" 
     android:background="#a8a8a8" > 

     <TableRow 
      android:id="@+id/tablerow_dialog_button" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      tools:ignore="UselessParent" > 

      <Button 
       android:id="@+id/button_dialog_yes" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="8sp" 
       android:paddingTop="5sp" 
       android:paddingBottom="5sp" 
       android:background="@drawable/roundedcornerbuttonfordialog_shape" 
       android:text="@string/string_yes" /> 

      <Button 
       android:id="@+id/button_dialog_no" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="8sp" 
       android:paddingTop="5sp" 
       android:paddingBottom="5sp" 
       android:background="@drawable/roundedcornerbuttonfordialog_shape" 
       android:text="@string/string_no" /> 

     </TableRow> 
    </TableLayout> 

</RelativeLayout> 

現在寫對話的代碼 -

public static void callAlert(String message, final Context context){ 
    final Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.customdialog_layout); 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));    

    TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); 
    tvTitle.setText("MyApp.."); 

    TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); 
    tvText.setText(message);  

    Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); 
    buttonDialogYes.setOnClickListener(new OnClickListener() {   
     public void onClick(View v) { 
      // Do your stuff... 
      dialog.dismiss(); 
     } 
    }); 

    Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); 
    buttonDialogNo.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // Do your stuff... 
      dialog.dismiss(); 
     }   
    }); 
    dialog.show(); 
} 

而且調用此方法 -

String message = "Your Message"; 
callAlert(message, callingClass.this); 

希望這會成爲你的希望你呢。

+1

與您一樣,使用對話框而不是AlertDialog解決了問題。謝謝! :D – 2013-03-02 13:03:37

+0

IMO,我不會使用TableLayout來嘗試和格式化。我會堅持線性,相對和框架。平整您的視圖層次結構會提高性能。 – jpotts18 2014-02-25 23:37:55

-1

爲什麼你不使用你顯示的圖像像「這是我的目標:」將你的目標圖像保存在可繪製文件夾中。

private AlertDialog createAlertDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(getLayoutInflater().inflate(R.layout.playdialog, null)); 
    return builder.create(); 
} 

然後

AlertDialog dialog = createAlertDialog(); 
dialog.show(); 
+0

由於可擴展性以及計算重新渲染時間和不必要的功耗等原因,建議不要使用圖像。 – computingfreak 2016-11-21 03:23:53

0

下面的代碼解決了這個問題

MyDialog mydialog = new MyDialog(this, "for testing", 
      new myOnClickListener() { 

       @Override 
       public void onPositiveButtonClick() { 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), 
          "I am positive button in the dialog", 
          Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void onNegativeButtonClick() { 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), 
          "I am negative button in the dialog", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 

    // this will remove rectangle frame around the Dialog 
    mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
    mydialog.show(); 

謝謝, Nagendra

+0

它沒有消除corder中的白色部分:( – sabbir 2015-01-07 20:32:12

相關問題