2013-05-31 241 views
17

我正在試圖製作一個帶圓角的自定義android對話框。我目前的嘗試給了我這個結果。Android對話框 - 圓角和透明度

rounded corner dialog

正如你所看到的,邊角圓潤,但它留下的白角仍然完好無損。

下面是我放置在可繪製文件夾中的xml,用於創建具有圓角的紅色邊框的藍色對話框。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape 
      android:shape="rectangle"> 
      <solid android:color="@color/transparent_black" /> 
      <corners android:radius="@dimen/border_radius"/> 
     </shape> 
    </item> 
    <item 
     android:left="@dimen/border_width" 
     android:right="@dimen/border_width" 
     android:top="@dimen/border_width" 
     android:bottom="@dimen/border_width" > 

     <shape android:shape="rectangle"> 
      <solid android:color="@color/blue" /> 
      <corners android:radius="@dimen/border_radius"/> 
     </shape> 
    </item>  
</layer-list> 

下面是對話框的佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
style="@style/fill" 
android:orientation="vertical" 
android:layout_margin="@dimen/spacing_normal" 
android:padding="@dimen/spacing_normal" 

android:background="@drawable/border_error_dialog" > 

<RelativeLayout 
    style="@style/block" 
    android:layout_gravity="center" > 

    <ImageView 
     android:id="@+id/imageView1" 
     style="@style/wrap" 
     android:layout_alignParentLeft="true" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/content_description_filler" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView1" 
     style="@style/error_text" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/imageView1" 
     android:text="@string/error_login" /> 

</RelativeLayout> 

<Button 
    android:id="@+id/button1" 
    style="@style/wrap" 
    android:layout_gravity="center" 
    android:text="Button" /> 

</LinearLayout> 

下面是我創建對話框的活動。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button b1 = (Button) findViewById(R.id.button1); 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) {    
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); 

      View child = getLayoutInflater().inflate(R.layout.dialog_custom_tom, null); 
      alertDialogBuilder.setView(child); 

      AlertDialog alertDialog = alertDialogBuilder.create(); 

      alertDialog.show(); 
     } 
    }); 
} 

回答

1

在這些角落使用具有透明度的9貼片PNG。

+0

任何代碼示例? – Talha

+0

這不是源代碼解決方案。這是一個9貼片PNG https://developer.android.com/studio/write/draw9patch.html –

10

我不得不做的時候擴大對話和DialogFragment這個用於固定的類似問題:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0); 

像這樣:

public class ConfirmBDialog extends DialogFragment { 

public static ConfirmBDialog newInstance() { 
    ConfirmBDialog dialog = new ConfirmBDialog(); 
    Bundle bundle = new Bundle(); 
    dialog.setArguments(bundle); 

    dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0); 

    return dialog; 
} 


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

    View view = inflater.inflate(R.layout.confirm_dialog, container, true); 
    getDialog().setCanceledOnTouchOutside(true); 
    return view; 
} 

}

希望這有助於。

+1

@greatergoodguy,請如果這個答案是你的問題的解決方案,接受它 – RiadSaadi

+0

經過多年的嘗試這是唯一的修復這對我有效!謝謝! – andrew

+0

完美的作品,謝謝!這是我發現的與DialogFragments一起使用的唯一解決方案... – Aenadon

26

我發現的唯一解決方案是here。使用對話框代替AlertDialog並設置透明背景:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
因此,您不能使用該構建器。但是,如果遵循最佳指導方針,則還可以在DialogFragment的onCreateDialog回調中使用新的Dialog()。

這也適用於薑餅。

除了分層繪製可以簡化爲一個形狀與xml元素<行程>爲邊界。

+1

更改爲對話框而不是AlertDialog終於爲我解決了這個問題。謝謝我的朋友:) – Shirane85

+0

它是如何爲彈出窗口 –

6

試試看

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
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

2

在你的java文件保存下面的代碼並更改佈局名稱

View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
+0

它的工作。謝謝..☺️ – SamSol

0
public void initDialog() { 
    exitDialog = new Dialog(this); 
    exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    View view = View.inflate(this, R.layout.dialoglayout, null); 
    exitDialog.setContentView(view); 
    AdSize adSize = new AdSize(300, 250); 

    dialogAdview = new AdView(this); 
    dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id)); 
    dialogAdview.setAdSize(adSize); 
    RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout); 
    adLayout.addView(dialogAdview); 
    AdRequest adRequest = new AdRequest.Builder() 
      .build(); 
    dialogAdview.loadAd(adRequest); 
    dialogAdview.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      Log.d("Tag", "adLoaded"); 
      super.onAdLoaded(); 
     } 


    }); 

    view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      exit = true; 
      onBackPressed(); 
     } 
    }); 

    view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      exit = false; 
      exitDialog.dismiss(); 
     } 
    }); 

} 

dialoglayout。XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/custom_dialog_round" 
    android:orientation="vertical"> 


    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:text="Do you want to exit?" 
     android:textColor="#000" 
     android:textSize="18dp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/yes_btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/background_draw" 
       android:padding="8dp" 
       android:text="Yes" 
       android:textAlignment="center" 
       android:textColor="#9fa8da" 
       android:textSize="20dp" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/no_btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="10dp" 
       android:background="@drawable/background_draw" 
       android:padding="8dp" 
       android:text="No" 
       android:textAlignment="center" 
       android:textColor="#d50000" 
       android:textSize="20dp" /> 
     </LinearLayout> 


    </LinearLayout> 

</LinearLayout> 
` 

custom_dialog_round.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid 
     android:color="#fff"/> 
    <corners 
     android:radius="10dp" /> 
    <padding 
     android:left="10dp" 
     android:top="10dp" 
     android:right="10dp" 
     android:bottom="10dp" /> 
</shape> 

參考http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html