【问题标题】:How to make this simple dialog fragment semi-transparent如何使这个简单的对话框片段半透明
【发布时间】:2013-07-07 18:47:53
【问题描述】:

我正在尝试使这个简单的对话框半透明:

class TestDialog extends SherlockDialogFragment
{


    public TestDialog()
    {
    super();        
    }




    @Override
    public Dialog onCreateDialog(Bundle savedInstanceStateBundle ) 
    {

    AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater1 = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder1.setView(inflater1.inflate(R.layout.dlg_test, null))
    // Add action buttons
    .setPositiveButton( "Ok", new DialogInterface.OnClickListener() 
     {

        @Override       
        public void onClick(DialogInterface dialog, int id)         
        {           
            // sign in the user ...                    
        }


     })
     .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() 
      {      
                 @Override
         public void onClick(DialogInterface dialog, int id)             
         {           

         }           
      });

        return builder1.create();

    }        


}

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="User name" />

 </LinearLayout>

我已经尝试了所有方法,从使用透明 .png 作为线性布局的背景可绘制对象,到将 alpha 字段设置为 0.5,再到使用等于 0 的颜色作为可绘制对象。我无法制作该对话框半透明。我不知道是否有可能。 我想要创建的是一个类似于下图中面板的对话框:。 谢谢。 注1:需要的min sdk是8,target是最新的(其实是v17)。

【问题讨论】:

  • 我认为这可能取决于 SDK 版本。在 1.6 中,默认情况下对话框是半透明的,但请尝试 AlertDialog.Builder(Context context, int theme),其中主题是 `android.R.style.Theme_Dialog。
  • 您是否尝试过在 XML 中使用类似 android:background="#4000" 的内容? (#4000 是 ARGB_4444 颜色空间)。
  • 我都试过了,但都没用。

标签: android actionbarsherlock android-dialogfragment


【解决方案1】:

代替:

return builder1.create();

试试这个:

Dialog dialog = builder1.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
return dialog;

【讨论】:

  • 它没有用。对话框总是黑得要命!看看我的帖子更新,看看我想成为什么样的对话框。
【解决方案2】:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    return dialog;
}

你可以放

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

在对话框的 onCreateView 中也是如此。

【讨论】:

    【解决方案3】:

    这就是我使用 AlertDialog 实现半透明所做的。

    创建了自定义样式:

    <style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
        <item name="android:colorBackground">#32FFFFFF</item>
    </style>
    

    然后创建对话框:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
    AlertDialog dialog = builder.create();
    

    链接到我对相关问题的回答:https://stackoverflow.com/a/36311627/4344057

    【讨论】:

      【解决方案4】:

      您可以在对话框片段参考中尝试这两个主题中的任何一个。

      yourdialogFragment.setStyle(style,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
      

      yourdialogFragment.setStyle(style,android.R.style.Theme_Holo_Light_Dialog);
      

      希望这会有所帮助。

      【讨论】:

        【解决方案5】:

        您可以尝试使背景透明,如果您的超类是对话框,请使用它:

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

        或者如果你的超类是 DialogFragment 的话:

        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        

        我的另一个解决方案类似于“kaushal trivedi”一个。 使用 DialogFragments:

        public class MyDialogFragment extends DialogFragment {
        
        ...
        
          @Override
          public void onCreate(Bundle savedInstance){
            super.onCreate(savedInstance);
            setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
          }
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.my_dialog, container,false);
          }
        }
        

        这使您的对话框全屏且没有背景,因此您可以在其中放入任何您想要的内容。

        【讨论】:

        • 如果您希望对话框和窗口之间有一个边距,这不是一个可行的解决方案。
        【解决方案6】:

        试试这个

        public class Dialogs extends DialogFragment {
        
            public Dialogs() {
            }
        
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style. Theme_WallpaperSettings);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多