开发者

Custom Dialog without title and border

开发者 https://www.devze.com 2023-02-19 01:10 出处:网络
Based on the code here, http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog . I am successfully able to create a custom dialog with background and buttons inside, but there\'s still

Based on the code here, http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog . I am successfully able to create a custom dialog with background and buttons inside, but there's still something not right. there still a space for title bar, and there are border around the view. how to get rid of these title and border?

here is my dialog

Dialog pauseMenu = new Dialog(this,R.xml.pause_dialog);
pauseMenu.setContentView(R.layout.pause_menu);
return pauseMenu;

and here is my pause layout

<?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&开发者_StackOverflow中文版gt;
</LinearLayout>


I think this will help you out

gameOver would be the dialog name and in setContentView it would be ur custom dialog layout

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


A Dialog cannot be created without a title. Further down in that tutorial it mentions:

A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.

This answer solves both the title and the border problem using a custom style.


  1. Make a class like this:

    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));
        }
    }
    
  2. make a xml in layut folder with this name dialog

    <?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>
    
  3. add the image above in your drawable folder with name bg_custom_dialog.9.png

Custom Dialog without title and border

  1. call in your activity:

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


Dialog dialog = new Dialog(Main.this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


Try the following style in your 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>

For more options see "Theme.Holo.Light.Dialog" which is part of the Android sources (since Honeycomb), or any other dialog style you based your's on.

Then simply use this style as basis to your own dialog:

Dialog myDialog = new Dialog(getActivity(), R.style.free_floating_dialog);
myDialog.setContentView(R.layout.myContent);


Use this, It works perfectly;

             dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before     
             dialog.setContentView(R.layout.your_dialog_layout);


I use this code and it work fine:

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

精彩评论

暂无评论...
验证码 换一张
取 消