【问题标题】:AlertDialog to wrap it's contentAlertDialog 来包装它的内容
【发布时间】:2015-12-06 13:48:21
【问题描述】:

我有一个自定义的View 用于我的AlertDialog 的标题和内容,这是视图:

view_tip.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/StandardLinearLayout"
    android:layout_height="match_parent" android:layout_width="match_parent">

    <TextView
        android:maxWidth="245sp"
        android:id="@+id/actionTip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</LinearLayout>

我希望AlertDialog 包装它的内容。我一直在关注这个帖子的解决方案:AlertDialog with custom view: Resize to wrap the view's content 但它们都不适合我。

方案一,完全没有效果,AlertDialog 占满整个空间:

// Scala code
val title = getLayoutInflater.inflate(R.layout.view_tip, null)
title.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "title"

val view = getLayoutInflater.inflate(R.layout.view_tip, null)
view.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "content"

val dialog = new android.app.AlertDialog.Builder(this).setCustomTitle(title).setView(view).show
dialog.getWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

解决方案 2 使用 forceWrapContent 修改视图层次结构,对内容有影响但标题不受影响:

// Scala code
val title = getLayoutInflater.inflate(R.layout.view_tip, null)
title.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "title"

val view = getLayoutInflater.inflate(R.layout.view_tip, null)
view.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "content"

val dialog = new android.app.AlertDialog.Builder(this).setCustomTitle(title).setView(view).show
forceWrapContent(title)
forceWrapContent(view)

...
// Java code
static void forceWrapContent(View v) {
        // Start with the provided view
        View current = v;

        // Travel up the tree until fail, modifying the LayoutParams
        do {
            // Get the parent
            ViewParent parent = current.getParent();

            // Check if the parent exists
            if (parent != null) {
                // Get the view
                try {
                    current = (View) parent;
                } catch (ClassCastException e) {
                    // This will happen when at the top view, it cannot be cast to a View
                    break;
                }

                // Modify the layout
                current.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
        } while (current.getParent() != null);

        // Request a layout to be re-done
        current.requestLayout();
    }

是否有任何其他解决方案,或者我可以以某种方式修改现有的解决方案以使其正常工作?

【问题讨论】:

    标签: android layout android-alertdialog


    【解决方案1】:

    您可以使用自定义对话框代替 AlertDialog

    这是自定义对话框

    的示例
        custom_dialog = new Dialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth);
        custom_dialog.getWindow().setBackgroundDrawable(new ColorDrawable((0xff000000)));
        custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        custom_dialog.setCancelable(false);
        custom_dialog.setContentView(R.layout.your_custom_layout);
        custom_dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Color.parseColor("#FFFFFF"));
        custom_dialog.show();
    

    如果上述逻辑不起作用,您可以使用Android Popup Window。它可用于显示任意视图,在您想要显示附加信息的情况下非常方便。

    如何创建 PopupWindow

    为包含创建自定义弹出 xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Test Pop-Up"
        />
    
    </LinearLayout>
    

    Java 代码:

    LayoutInflater inflater = (LayoutInflater)
       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    PopupWindow pw = new PopupWindow(
       inflater.inflate(R.layout.popup_example, null, false), 
       100, 
       100, 
       true);
    // The code below assumes that the root container has an id called 'main'
    pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0); 
    

    Code Courtesy.

    出于演示目的,您可以查看 Git AlertDialogProCustomAlertDialog。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      使用窗口管理器

      private WindowManager wm;
      wm = (WindowManager) arg0.getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
      params1 = new WindowManager.LayoutParams(
          RelativeLayout.LayoutParams.MATCH_PARENT,
          RelativeLayout.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION |
          WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
          WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
          WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
          PixelFormat.TRANSPARENT);
      params1.height = (height/3);
      params1.width = width;
      params1.x = 0;
      params1.y = 0;
      params1.format = PixelFormat.TRANSLUCENT;
      ly1 = new LinearLayout(arg0.getApplicationContext());
      ly1.setOrientation(LinearLayout.HORIZONTAL);
      ly1.addView(hiddenInfo);
      wm.addView(ly1, params1);
      

      【讨论】:

        【解决方案3】:

        这看起来像你的第一个解决方案,但它有点不同 - 也许它是这样写的 - link

        如果这不起作用,您可以创建自己的 Dialog 类,它使用 popuWindow - 如果您愿意,我可以向您展示我的一些代码以帮助您入门。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-26
          • 2012-05-26
          • 1970-01-01
          相关资源
          最近更新 更多