【问题标题】:custom dialog with close button带有关闭按钮的自定义对话框
【发布时间】:2013-11-28 05:56:40
【问题描述】:

我想创建一个自定义对话框,其布局如图所示。 交叉/关闭按钮必须位于右上角。 请建议我如何实现这种布局。

提前致谢。

【问题讨论】:

标签: android android-layout customdialog


【解决方案1】:

可能下面的代码对你有用,像这样实现的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/popup_password_latest"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="-50dp" >

          <!---add your views here-->
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:clickable="true"
            android:src="@drawable/close_selector" />
    </FrameLayout>

</LinearLayout>

【讨论】:

  • 我只面临一个问题,那就是我在对话框后面得到了白色背景。可以去掉吗?
  • 对主布局使用可绘制的背景,例如 schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:重力="center" android:orientation="vertical" >
  • 什么都不会解决,因为他说我们只能看到白色背景,这里我们在对话框中添加了透明度 dialog.getWindow().setBackgroundDrawable( new ColorDrawable(android.graphics.Color.TRANSPARENT ));
  • @IssacBalaji 谢谢,这解决了对话框背后的白色背景问题。
【解决方案2】:

相对布局将是您的父布局,然后添加关闭按钮 apply android:layout_alignParentRight="true"android:layout_alignParentTop="true" 甚至根据您的要求在减号中给出 右和上边距 .

代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="255dp"
        android:layout_height="385dp"
        android:layout_centerInParent="true"
        android:background="@color/white"
        android:orientation="vertical" >
    </LinearLayout>

    <Button
        android:id="@+id/button_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/relativeLayout1"
        android:layout_alignTop="@+id/relativeLayout1"
        android:layout_marginRight="-10dp"
        android:layout_marginTop="-10dp"
        android:background="@drawable/close" />

</RelativeLayout>

输出

【讨论】:

    【解决方案3】:

    我有一个解决方案。

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
    
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp" >
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/img_popup_one"
                android:orientation="vertical"
                android:padding="10dp" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sign In"
                    android:textColor="@color/clr_gray"
                    android:textSize="16dp" />
    
                <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/img_edittext_bg"
                    android:hint="Username"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:textColorHint="@color/clr_gray" />
    
                <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:background="@drawable/img_edittext_bg"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:textColorHint="@color/clr_gray" />
            </LinearLayout>
        </FrameLayout>
    
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="right|top"
            android:contentDescription="@string/contentDescription"
            android:src="@drawable/img_popup_close" />
    </FrameLayout>
    

    【讨论】:

    • FrameLayout 有 3 个但 XML 代码只关闭了两个?
    【解决方案4】:

    您可以在此处使用 PopupWindow。您可以为自定义对话框制作布局,并可以在 PopupWindow 中扩展该布局。它应该是这样的:-

    PopupWindow menuPopup;
    menuView=getLayoutInflater().inflate(R.layout.layout_menu, null);
    menuPopup=new PopupWindow(menuView, 200, 200, false);
    menuPopup.showAtLocation(menuView, Gravity.TOP | Gravity.RIGHT, 0, 100); 
    

    【讨论】:

    • 异常:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。
    【解决方案5】:

    将此用于自定义对话框...

     private PopupWindow pw;
    
    @SuppressWarnings("deprecation")
    private void initiatePopupWindow() {
    
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popup,
                (ViewGroup) findViewById(R.id.btncancelcat));
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        pw = new PopupWindow(layout, lp.width, lp.height, true);
        pw.showAtLocation(layout, Gravity.CENTER_VERTICAL, 0, 0);
    
    
        ImageButton btncancel = (ImageButton) layout.findViewById(R.id.btncancelcat);
    
        btncancel.setOnClickListener(cancel_click);
    
    }
    
    private OnClickListener cancel_click = new OnClickListener() {
        public void onClick(View v) {
            pw.dismiss();
    
        }
    };
    

    popup.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearpopup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#e22e2e"
        android:orientation="vertical"
        android:padding="10dp" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" >
        </LinearLayout>
    
        <ImageButton
            android:id="@+id/btncancelcat"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/btn" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案6】:

      ![anybody can try above all but u will be facing problem background color problem
      
      i have done very simple way 
      
      
      
        <?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"
           >
      
      
      
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="50dp"
                      android:layout_marginLeft="50dp"
                      android:layout_marginRight="50dp"
                      android:layout_marginTop="50dp"
                      android:background="#FFFFFF"
                      android:gravity="center"
                      android:orientation="horizontal" >
      
                      <LinearLayout
                          android:layout_width="120dp"
                          android:layout_height="match_parent"
                          android:layout_gravity="center"
                          android:gravity="center"
                          android:orientation="vertical" >
      
                          <ImageView
                              android:id="@+id/left_arrow_imageview"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:src="@drawable/up_arrow" />
      
                          <ListView
                              android:id="@+id/listview_observation"
                              android:layout_width="match_parent"
                              android:layout_height="0dp"
                              android:layout_gravity="center"
                              android:layout_weight="2"
                              android:padding="6dp" >
                          </ListView>
      
                          <ImageView
                              android:id="@+id/right_arrow_imageview"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:src="@drawable/down_arrow" />
                      </LinearLayout>
      
                      <View
                          android:layout_width="1dp"
                          android:layout_height="match_parent"
                          android:layout_gravity="center"
                          android:layout_marginLeft="3dp"
                          android:layout_marginRight="3dp"
                          android:background="#B2B2B2"
                          android:scrollY="@dimen/activity_vertical_margin" />
      
                      <LinearLayout
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="2"
                          android:gravity="right"
                          android:orientation="vertical" >
      
                          <TextView
                              android:id="@+id/text_head"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_gravity="left"
                              android:padding="6dp"
                              android:text="@string/Observation"
                              android:textSize="18sp"
                              android:textStyle="bold" />
      
                          <View
                              android:layout_width="match_parent"
                              android:layout_height="1dp"
                              android:layout_gravity="center"
                              android:layout_marginLeft="3dp"
                              android:layout_marginRight="3dp"
                              android:background="#B2B2B2"
                              android:scrollY="@dimen/activity_vertical_margin" />
      
                          <EditText
                              android:id="@+id/edit_observation"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:layout_weight="1"
                              android:background="#00000000"
                              android:ems="10"
                              android:gravity="top" >
                          </EditText>
      
                          <View
                              android:layout_width="match_parent"
                              android:layout_height="1dp"
                              android:layout_gravity="center"
                              android:layout_marginLeft="3dp"
                              android:layout_marginRight="3dp"
                              android:background="#B2B2B2"
                              android:scrollY="@dimen/activity_vertical_margin" />
      
                          <Button
                              android:id="@+id/button_done"
                              android:layout_width="94dp"
                              android:layout_height="wrap_content"
                              android:layout_marginRight="14dp"
                              android:layout_marginTop="3dp"    
                              android:background="@drawable/rectangular_view_selected"
                              android:text="@string/button_done"
                              android:textColor="@android:color/white" />
      
                      </LinearLayout>
                  </LinearLayout>
      
              <ImageView
                  android:id="@+id/icon_close"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentTop="true"
                  android:layout_marginRight="35dp"
                  android:layout_marginTop="35dp"
                  android:src="@drawable/icon_close" />
      
          </RelativeLayout>
      
      the dialog must be like that
      
         private void ChildSectionView() {
      
                  final Dialog dialog = new Dialog(NewObservationActivity.this);
                  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  dialog.setContentView(R.layout.custom_dialog_obs_children);
                  // Grab the window of the dialog, and change the width
                  dialog.getWindow().setBackgroundDrawable(
                          new ColorDrawable(android.graphics.Color.TRANSPARENT));
                  WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                  Window window = dialog.getWindow();
                  lp.copyFrom(window.getAttributes());
      
                  Display display = getWindowManager().getDefaultDisplay();
                  Point size = new Point();
                  display.getSize(size);
                  lp.width = size.x;
                  lp.height = size.y;
                  // This makes the dialog take up the full width
                  window.setAttributes(lp);
      
                  gridview = (GridView) dialog.findViewById(R.id.gridview_observation);
      
                  ImageView icon_close = (ImageView) dialog.findViewById(R.id.icon_close);
                  child_count = (TextView) dialog
                          .findViewById(R.id.text_child_count_dialog);
      
                  child_count.setText("Selected " + intList.size() + " more children");
                  SearchView searchview = (SearchView) dialog
                          .findViewById(R.id.search_children);
      
                  icon_close.setOnClickListener(new OnClickListener() {
      
                      @Override
                      public void onClick(View v) {
                          dialog.dismiss();
                      }
                  });
      
                  searchview.setOnQueryTextListener(new OnQueryTextListener() {
      
                      @Override
                      public boolean onQueryTextSubmit(String arg0) {
                          // TODO Auto-generated method stub
                          return false;
                      }
      
                      @Override
                      public boolean onQueryTextChange(String string) {
                          filItemAdapter("N", string);
                          return false;
                      }
                  });
      
                  filItemAdapter("", "");
      
                  dialog.show();
              }
      

      注意:--------

      the relative layout background should be transparent
      and dialog background also
      dialog.getWindow().setBackgroundDrawable(
      new ColorDrawable(android.graphics.Color.TRANSPARENT));][2]
      

      【讨论】:

        【解决方案7】:

        添加一个具有该背景的按钮并将位置alignParentTopalignParentRight 设置为true。并制作父布局Relative

        【讨论】:

          【解决方案8】:
          <?xml version="1.0" encoding="utf-8"?>
          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@null"
          xmlns:app="http://schemas.android.com/apk/res-auto">
          
          <YourLayout
              android:layout_marginTop="25sp"
               
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
          
          
          <com.google.android.material.floatingactionbutton.FloatingActionButton
              android:id="@+id/fabClose"
              android:tint="@color/white"
              app:fabCustomSize="40sp"
              app:maxImageSize="20sp"
              app:backgroundTint="@color/white"
              android:layout_gravity="center_horizontal"
              android:src="@drawable/ic_close"
              android:layout_width="40sp"
              android:layout_height="40sp"/>
          </FrameLayout>
          

          在 Kotlin 文件中写如下

              val alertDialog = dialogBuilder.create()
              alertDialog.show()
              alertDialog?.getWindow()?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
          

          这对我来说很好用

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-16
            • 1970-01-01
            • 1970-01-01
            • 2012-06-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-08
            相关资源
            最近更新 更多