Android DialogFragment TextView setText()不起作用

时间:2014-03-08 15:01:02

标签: android android-dialogfragment dialogfragment

这是我的onCreate()函数:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //inflate the view
    View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_loading, null);     
    TextView loadingMessage = (TextView) view.findViewById(R.id.fragment_loading_message_textview);
    loadingMessage.setText("test");
    //the dialog
    Dialog dialog;
    //set the dialog views
    int themeId = getDialogThemeId();
    //if there is no theme
    if (themeId == -1)
        dialog = new Dialog(getActivity());
    else
        dialog = new Dialog(getActivity(),getDialogThemeId()); 
    //remove background
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    //remove title
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //set the content view
    dialog.setContentView(view);        
    //set cancel properties
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);
    return dialog;
}       

这是我正在膨胀的布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" 
    android:background="@android:color/black">

    <!-- spinner -->

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent" />

    <!-- text -->

    <TextView
        android:id="@+id/fragment_loading_message_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="@android:color/white" 
        android:text="LOADING"/>

</LinearLayout>

虽然我正在调用loadingMessage.setText(“test”),但加载消息仍在显示消息中的“LOADING”...任何想法???

1 个答案:

答案 0 :(得分:3)

好的,我发现了这个问题。 在同一个DialogFragment中我也实现了onCreateDialog和onCreateView。

这导致我的视​​图被夸大两次。因为在onCreateDalog之后调用onCreateView,所以我在onCreateDialog中加载的视图被覆盖了..

希望有其他人