以编程方式向对话片段添加按钮永远不会显示



所以我试图以编程方式向DialogFragment添加一个按钮,但该按钮从未显示。

我有以下布局:

<?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;
 }

问题:

问题是我添加的按钮在启动后永远不会显示在布局中。几乎似乎在我添加按钮之前创建了布局,因此按钮永远不会显示......

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

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

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

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

最新更新