簡體   English   中英

替換片段時不顯示布局

[英]Layout does not appear when replacing a fragment

我正在嘗試用其他容器替換容器中的片段。 我在顯示的新片段中做了一個Toast,但是沒有顯示布局。

這是我替換片段的位置:

 Bundle bundle = new Bundle();
            bundle.putString("title", textContentList.get(position).getTitle());
            bundle.putString("crated", textContentList.get(position).getCreated());
            bundle.putString("author", textContentList.get(position).getAuthor());
            bundle.putString("fullContent", textContentList.get(position).getFullContent());
            MessageDetailsFragment messageDetailsFragment = new MessageDetailsFragment();
            messageDetailsFragment.setArguments(bundle);
            replaceFragment(messageDetailsFragment);

public void replaceFragment(Fragment fragment){
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.container, fragment);
    transaction.commit();
}

這是我的片段布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_container"
android:orientation="vertical"
android:layout_marginTop="70dp"
android:background="?android:colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    style="@style/headings"
    android:text="Author"
    android:layout_marginTop="10dp"
    android:id="@+id/message_details_author"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Receiver"
    android:id="@+id/message_details_receiver"/>

<View
    android:layout_width="320dp"
    android:layout_gravity="center"
    android:layout_height="1dp"
    style="@style/dividerLine" />

<TextView
    style="@style/headings"
    android:layout_marginTop="10dp"
    android:text="Title"
    android:id="@+id/message_details_title"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Date"
    android:id="@+id/message_details_date"/>

<View
    android:layout_width="320dp"
    android:layout_gravity="center"
    android:layout_height="1dp"
    style="@style/dividerLine" />

<TextView
    android:text="Full content"
    style="@style/subTxt_messageDetails"
    android:layout_marginTop="15dp"
    android:id="@+id/message_details_full_content"/>

<TextView
    style="@style/subTxt_messageDetails"
    android:text="Sent by SMS"
    android:textSize="15sp"
    android:layout_marginTop="15dp"
    android:id="@+id/message_sent_by"/>

</LinearLayout>

這是我的新片段:

public class MessageDetailsFragment extends Fragment {

private String title, created, author, fullContent;
private TextView textViewTitle, textViewDate, textViewAuthor, textViewFullContent;
private Bundle bundle;
private Context context;

public MessageDetailsFragment(){

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bundle = this.getArguments();
    title = bundle.getString("title");
    created = bundle.getString("created");
    author = bundle.getString("author");
    fullContent = bundle.getString("fullContent");
    context = getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_message_details, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    Toast.makeText(context, "ADDASD", Toast.LENGTH_SHORT).show();

    textViewTitle = (TextView) getView().findViewById(R.id.message_details_title);
    textViewTitle.setText(title);
    textViewDate = (TextView) getView().findViewById(R.id.message_details_date);
    textViewDate.setText(created);
    textViewAuthor = (TextView) getView().findViewById(R.id.message_details_author);
    textViewAuthor.setText(author);
    textViewFullContent = (TextView) getView().findViewById(R.id.message_details_full_content);
    textViewFullContent.setText(fullContent);

}
}

您的樣本是不可復制的,因為它指的是未提供的樣式等,因此一般來說很難確定。

要調試Fragment布局,請首先清除除父ViewGroup之外的所有內容。 為它提供易於識別的顏色,以便在啟動時可以看到它實際上已出現在您的應用中。

如果出現,則問題在於您的布局。 如果沒有,則很可能是您的代碼。

完成此操作后,您可以逐漸添加其他視圖。

例如

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="@color/Red"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 </LinearLayout

感謝所有的答案。 原來,我試圖將片段插入到viewpager中,我也在應用程序中使用了它。 通過創建一個新的FrameLayout並將其插入其中,我解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM