Android 没有标题和边框的自定义对话框
根据这里的代码, . 我成功地创建了一个带有背景和按钮的自定义对话框,但仍然有一些不对劲。标题栏还有一个空间,视图周围还有边框。如何摆脱这些标题和边界 这是我的对话Android 没有标题和边框的自定义对话框,android,layout,Android,Layout,根据这里的代码, . 我成功地创建了一个带有背景和按钮的自定义对话框,但仍然有一些不对劲。标题栏还有一个空间,视图周围还有边框。如何摆脱这些标题和边界 这是我的对话 Dialog pauseMenu = new Dialog(this,R.xml.pause_dialog); pauseMenu.setContentView(R.layout.pause_menu); return pauseMenu; 这是我的暂停布局 <?xml version="1.0" encoding="utf
Dialog pauseMenu = new Dialog(this,R.xml.pause_dialog);
pauseMenu.setContentView(R.layout.pause_menu);
return pauseMenu;
这是我的暂停布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:background="@drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="@drawable/pause_button_quit" android:layout_width="wrap_content" android:background="@drawable/pause_button_quit" android:id="@+id/imageButton2" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:src="@drawable/pause_button_option" android:layout_width="wrap_content" android:background="@drawable/pause_button_option" android:id="@+id/imageButton1" android:layout_height="wrap_content"></ImageButton>
</TableLayout>
</LinearLayout>
如果没有标题,则无法创建对话框。在该教程的后续部分,它提到: 使用基本对话框创建的对话框 类必须有一个标题。如果你不 调用setTitle(),然后使用 因为标题仍然为空,但仍然为空 看得见的如果你不想要一个头衔 所有,那么你应该创建你的 使用AlertDialog的自定义对话框 班级。但是,由于一个AlertDialog 使用 AlertDialog.Builder类,您不需要 有权访问setContentView(int) 上述方法。相反,你必须 使用setView(视图)。此方法接受 视图对象,因此需要充气 布局的根视图对象 XML
使用自定义样式解决标题和边框问题。我认为这将对您有所帮助 gameOver将是对话框名称,在setContentView中,它将是自定义对话框布局
gameOver = new Dialog(Main.this);
gameOver.requestWindowFeature(Window.FEATURE_NO_TITLE);
gameOver.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
gameOver.setCancelable(false);
gameOver.setContentView(R.layout.gameover);
public class CustomDialog extends Dialog {
public AlertFinishiView(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bg_custom_dialog" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
在styles.xml中尝试以下样式:
<style name="free_floating_dialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
使用这个,它可以完美地工作
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
dialog.setContentView(R.layout.your_dialog_layout);
我使用此代码,它工作正常:
AlertDialog.Builder builder = new AlertDialog.Builder(contextActivity,android.R.style.Theme_Holo_Dialog_NoActionBar);
AlertDialog alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.setView(myLayout,0,0,0,0);
alertDialog.show();
可能是重复的我正要发布一个类似的问题,下面清楚地回答了解决方案。看起来我理解标题,但是关于边框的哪一部分?答案中的这行处理:@color/transparent\u white代替AlertDialog我们可以通过
final Dialog=new Dialog解决这个问题(ShortcutActivity.this);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
在4.1以下的android版本中,对话框宽度覆盖了全屏,这对UII不利。我同意@basavarajampali,这解决了问题,但宽度占据了整个宽度……我想知道如何解决这个问题?这不起作用!!:(myDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);+1非常好和简洁的代码。这确实是一个技巧!非常感谢您与我们分享它。
<style name="free_floating_dialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
Dialog myDialog = new Dialog(getActivity(), R.style.free_floating_dialog);
myDialog.setContentView(R.layout.myContent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
dialog.setContentView(R.layout.your_dialog_layout);
AlertDialog.Builder builder = new AlertDialog.Builder(contextActivity,android.R.style.Theme_Holo_Dialog_NoActionBar);
AlertDialog alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.setView(myLayout,0,0,0,0);
alertDialog.show();