AlertDialog包装它的内容

时间:2015-09-10 10:15:11

标签: android layout alertdialog

我有一个自定义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但是它们都不适合我。

解决方案1,完全没有效果,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)

enter image description here

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

// 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();
    }

enter image description here

是否还有其他解决方案,或者我可以以某种方式修改现有解决方案以使其有效?

2 个答案:

答案 0 :(得分:5)

您可以使用自定义对话框代替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

为contains

创建自定义弹出式窗口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。我希望它会对您有所帮助。

答案 1 :(得分:0)

使用窗口管理器

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