所以我尝试以编程方式向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;
}
问题:
问题是我添加的按钮一旦启动就不会在布局中显示。在添加按钮之前,几乎看起来布局已经创建,因此按钮永远不会显示...
答案 0 :(得分:0)
问题是我添加的按钮从未显示在 布局一旦启动
因为忘记在传递relLayout
的对话框布局中添加dialog.getWindow().setContentView
。
使用view.addView
方法在view
中添加按钮:
relLayout.addView(button1);
view.addView(relLayout);