25

角が丸いカスタム android ダイアログを作成しようとしています。私の現在の試みは、この結果をもたらしました。

角丸ダイアログ

ご覧のとおり、角は丸くなっていますが、白い角はそのままです。

以下は、丸みを帯びた角を持つ赤い境界線を持つ青いダイアログを作成するために、drawable フォルダーに入れた 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();
        }
    });
}
4

11 に答える 11

34

私が見つけた唯一の解決策はhereです。AlertDialog の代わりに Dialog を使用し、透明な背景を設定します。
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
したがって、ビルダーを使用することはできません。ただし、最良のガイドラインに従えば、DialogFragment の onCreateDialog コールバックでも new Dialog() を使用できます。

これはジンジャーブレッドでも機能します。

さらに、レイヤー化されたドローアブルは、ボーダーの xml 要素 <stroke> を使用して 1 つの形状に単純化できます。

于 2014-05-03T23:17:20.343 に答える
3

あなたのJavaファイルでは、以下のコードを保持し、レイアウト名を変更してください

View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
于 2016-11-11T12:11:39.657 に答える
3

以下のコードは問題を解決しました

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();

ありがとう、ナゲンドラ

于 2014-12-02T12:40:49.857 に答える
2

Use 9-patch PNG with transparency in those corners.

于 2013-05-31T16:04:23.647 に答える
1
  • アップデート

活動の背景が理にかなっていることがわかりました。したがって、これらの変更で@robertの回答を使用してください。

レイアウトでDialogFragment幅と高さを設定するか、最小サイズを追加します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content" // Or match_parent, 300dp.
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:background="@drawable/white_round_corner_background"
    android:gravity="center"
    android:minWidth="300dp"
    android:minHeight="200dp"
    android:orientation="vertical"
    android:padding="15dp"
    >
...

必要なアクティビティのスタイルから削除<item name="android:background">@color/...</item>し、これらの背景をアクティビティのレイアウトに設定します。

書面でDialogFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This removes black background below corners.
    setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
  • 古いバリアント

ロバートの回答によると、適用する必要setStyle(STYLE_NO_FRAME, 0)がありますが、新しい問題が発生しています。Custom dialog too smallDialogFragmentのように狭い場合は、このガイドに従う必要があります。

styles.xmlダイアログのサイズについては、次の 3 行を追加してください。

<style name="ErrorDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:minWidth" type="dimen">300dp</item>
    <!-- This option makes dialog fullscreen and adds black background, so I commented it -->
    <!-- <item name="android:minHeight" type="dimen">200dp</item> -->
    <!-- This option doesn't work, so I commented it -->
    <!-- <item name="android:layout_width">match_parent</item> -->
</style>

DialogFragment追加スタイルのレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:minWidth="300dp" // Optional, remove this line.
    android:minHeight="200dp" // Optional, remove this line.
    style="@style/ErrorDialogTheme"
    android:theme="@style/ErrorDialogTheme"
    >

DialogFragmentあなたの書き込みのコードで:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // This removes black background. If not 0 as a parameter, black background will appear.
    setStyle(STYLE_NO_FRAME, 0)
}

// If you want a fullscreen dialog, use this, but it doesn't remove a black background.
override fun onStart() {
    super.onStart()
    dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT)
}

AndroidManifest.xmlこれらのダイアログを表示できるすべてのアクティビティを調べて見つけ、テーマを確認しandroid:theme="..."て、 に進みstyles.xmlます。<item name="android:background">@color/...</item>それでは、これらのテーマのアイテムを見てみましょう。透明な色があるはずです。そうでない場合、これらのアイテムは存在しない可能性があります。これらの背景アイテムがある場合、アクティビティ全体にもそれらの背景とダイアログが含まれます。したがって、そのDialogFragment上にカメラ アクティビティがある場合は、これが表示されます。

ここに画像の説明を入力

必要なスタイルの背景アイテムを削除します。また、コードで背景が設定されている可能性があります。確認してください。

Android の透明な背景を持つダイアログと多くのページでは、次のいずれかを追加するように記述されています。

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

おそらくonViewCreated()またはにあるのですが、 の背景がスタイルで設定されていonCreateDialog()たため、役に立ちませんでした。Activity

Android 5.0.1 を実行する Samsung Galaxy S4 でテスト済み。

于 2018-10-08T10:49:36.637 に答える