Android 以编程方式向DialogFragment添加按钮永远不会显示

Android 以编程方式向DialogFragment添加按钮永远不会显示,android,android-layout,dialog,fragment,Android,Android Layout,Dialog,Fragment,因此,我试图通过编程方式将一个按钮添加到对话框片段,但该按钮从未显示 我有以下布局: <?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" a

因此,我试图通过编程方式将一个按钮添加到
对话框片段
,但该按钮从未显示

我有以下布局:

<?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"
    android:background="#99000000"
    android:padding="10dp">
    <RelativeLayout
        android:id="@+id/notificationLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/logo_background_shape"
        android:padding="2dp"
        android:layout_centerVertical="true"
        >
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/input_layout"
            android:paddingBottom="20dp">
            <TextView
                android:id="@+id/start_date_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="Some text"
                android:paddingLeft="10dp"
                android:textColor="@color/my_blue"/>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/clickables_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/input_layout"
            android:paddingBottom="10dp">
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
 * Setup the dialog and its styles
 */
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);

setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

final View view = getActivity().getLayoutInflater().inflate(R.layout.notification_fragment_layout, null);

final RelativeLayout relLayout = (RelativeLayout)getActivity().findViewById(R.id.clickables_layout);

RelativeLayout.LayoutParams paramsButton1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsButton1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button button1 = (Button) mDynamicNotification.getClickableList().get(0).getElementView(getActivity(), paramsButton1);
relLayout.addView(button1);

/**
 * Code block handling blurring of the background
 */
final Drawable d = new ColorDrawable(Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);

//allow the dialog to be canceled when touching outside of the dialog
dialog.setCanceledOnTouchOutside(false);

final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
    Bitmap image = BlurrBuilder.blur(content);
    dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else
    content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap image = BlurrBuilder.blur(content);
            dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
        }
    });
return dialog;
 }
问题:

<?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"
    android:background="#99000000"
    android:padding="10dp">
    <RelativeLayout
        android:id="@+id/notificationLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/logo_background_shape"
        android:padding="2dp"
        android:layout_centerVertical="true"
        >
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/input_layout"
            android:paddingBottom="20dp">
            <TextView
                android:id="@+id/start_date_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="Some text"
                android:paddingLeft="10dp"
                android:textColor="@color/my_blue"/>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/clickables_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/input_layout"
            android:paddingBottom="10dp">
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
 * Setup the dialog and its styles
 */
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);

setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

final View view = getActivity().getLayoutInflater().inflate(R.layout.notification_fragment_layout, null);

final RelativeLayout relLayout = (RelativeLayout)getActivity().findViewById(R.id.clickables_layout);

RelativeLayout.LayoutParams paramsButton1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsButton1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button button1 = (Button) mDynamicNotification.getClickableList().get(0).getElementView(getActivity(), paramsButton1);
relLayout.addView(button1);

/**
 * Code block handling blurring of the background
 */
final Drawable d = new ColorDrawable(Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);

//allow the dialog to be canceled when touching outside of the dialog
dialog.setCanceledOnTouchOutside(false);

final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
    Bitmap image = BlurrBuilder.blur(content);
    dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else
    content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap image = BlurrBuilder.blur(content);
            dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
        }
    });
return dialog;
 }
问题是,我添加的按钮一旦启动,就不会显示在布局中。看起来布局是在我添加按钮之前创建的,所以按钮永远不会显示

问题是,我添加的按钮从未显示在 布局一旦启动

因为忘记在对话框布局中添加
relLayout
,该对话框布局传入
Dialog.getWindow().setContentView

使用
view.addView
方法在
view
中添加按钮:

relLayout.addView(button1);
view.addView(relLayout);

不幸的是,addView方法不是View对象@GeorgiAngelov:ok的成员,请使用
ViewGroup
而不是
View