13

カスタム ダイアログ ボックスを透明にする必要があります。

サンプルコード:

Dialog dialog;
@Override
protected Dialog onCreateDialog(int id) 
{ 
    switch(id) 
    {
        case 1:
            AlertDialog.Builder builder = null;
            Context mContext = this;
            LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( LAYOUT_INFLATER_SERVICE );
            View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );
            builder = new AlertDialog.Builder( mContext );
            builder.setView( layout );
            dialog = builder.create();
            break;
    }
    return dialog;
}

透過ダイアログボックスの設定方法を教えてください。

私のXMLコード:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/about_Root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/alert_page_border"
    android:orientation="vertical" >        
    <TextView 
        android:id="@+id/about_Title"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="25dip"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:text="Welcome" />
</LinearLayout>

コード Alert_page_border.xml

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

サンプルのスクリーンショット:

ここに画像の説明を入力

このブロックの色を回避するにはどうすればよいですか。

4

8 に答える 8

52

AlertDialog の代わりにダイアログを使用する

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();
于 2013-03-06T11:01:17.467 に答える
4

API >= 11 を使用している場合は、次のようにダイアログにテーマを設定してみてください。

new AlertDialog.Builder(mContext , android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

または、背景を透明に設定することもできます:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
于 2012-08-08T12:24:28.453 に答える
2

スタイルを作成してダイアログに適用するだけです

    <style name="myDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/Transparent</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

次に、このテーマを使用してダイアログを作成します。

    Dialog myDialog = new Dialog(this,R.style.myDialogTheme) {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.about_page);
        }
    };

これがあなたの目標を達成するのに役立つことを願っています。

于 2012-08-08T12:29:49.920 に答える
1

あなたのこの質問は、私の問題が何であったかを正確に説明しています。このスレッドだけでなく、さらにいくつかのスレッドで見つけたすべての解決策を試しました。何も機能しませんでした。最後に、このスレッド対応する回答に出くわしました。

カスタム ダイアログ クラスを作成する必要がありました。だから私はそうしました、そしてそれはうまくいきました。実際には、次のスタイルで十分でした。

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent_black">#66000000</color>
</resources>

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="custom_dialog_theme" parent="@android:style/Theme.Translucent">
        <item name="android:windowBackground">@color/transparent_black</item>
        <item name="android:windowIsFloating">true</item>
    </style>
</resources>

ダイアログ クラスのコード:

import android.app.Dialog;
import android.content.Context;

import my.app.com.android.R;

public class CustomDialog extends Dialog {
    public AddBookCustomDialog(final Context context) {
        super(context, R.style.custom_dialog_theme);
        this.setContentView(R.layout.add_book_dialog);
    }
}

アクティビティ コードでこのダイアログを使用する方法:

CustomDialog customDialog = new CustomDialog(this);
customDialog.show();

テーマを考慮するためにカスタム クラスを作成する必要がある理由がわかりません。今、私がやりたかったこと、つまり透明な対話を達成しました。次の戦いは、AlertDialog.Builder. うまくいけば、私のコードがあなたの助けになるでしょう。

また、alpha の値が低いことに注意してください。構成が機能するかどうかを確認するために、00 で試してみてください。

于 2012-11-14T10:04:29.447 に答える
1

これを試して、

layout.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

この行の後に上記のコードを追加します。

View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );
于 2012-08-08T12:24:51.567 に答える
0
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/a"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:background="#00000000">

 <!-- Write your LinearLayout code here -->

 </RelativeLayout>

このコードを試してください。

于 2012-08-08T12:17:58.953 に答える
0

取り除くだけ

Alert_page_border.xml から

于 2014-04-10T13:04:29.880 に答える