2015-04-10 1 views
1

Так что я пытаюсь добавить кнопку программно в DialogFragment, но кнопка никогда не отображается.Добавление программ программно в 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> 

Вот как я создаю вид DIALOG Моего:

@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

Проблема заключается в том, что кнопка, которую я добавляю никогда не отображается в макет после запуска

Потому что забудьте добавить relLayout в макет Диалога, который проходит в dialog.getWindow().setContentView.

Используйте view.addView метод, чтобы добавить кнопку в view:

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

К сожалению, метод addView не является членом Посмотреть объект, –

+0

@GeorgiAngelov: ок затем использовать 'ViewGroup' вместо' View' –

Смежные вопросы