android - AlertDialog 包装它的内容

标签 android layout android-alertdialog

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

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)

enter image description here

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

// 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

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

最佳答案

您可以使用自定义对话框代替 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 .它可用于显示任意 View ,并且在您想要显示附加信息的情况下非常方便。

如何创建弹出窗口

为包含创建自定义弹出 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 .希望对您有所帮助。

关于android - AlertDialog 包装它的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32499077/

相关文章:

android - 自版本 6 起的 Android 设备上的选项按钮,菜单膨胀

android - DatePickerFragment+EditText 实现与 AlertDialog

android - Android Activity 中保存的实例状态未保留的保留 fragment

java - FrameLayout onClick 事件不起作用

java - 单击按钮时,如何将文本从警报对话框的 EditText 复制并粘贴到我的 Activity 的 EditText?

android - BroadcastReceiver 试图在无序广播期间返回结果 Weird Error

html - 获取输入以填充剩余宽度,如 span 元素

templates - 为什么我的 Magento 产品页面的评论部分没有显示?

android - 对话框出现后,ListView 内的 RadioButton 未选中

java - 在 Android 中将 AlertDialog Builder 限制为最多两行