替换片段时不显示布局

时间:2017-03-01 11:08:19

标签: java android xml layout

我正在尝试用不同的容器替换容器中的片段。我在显示的新片段中制作了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);

}
}

2 个答案:

答案 0 :(得分:0)

你的样品不具有可重复性,因为它指的是没有给出的样式等,因此一般来说很难确定......

要调试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

答案 1 :(得分:0)

感谢所有答案。事实证明我试图将片段插入到我也在应用程序中使用的viewpager中。通过创建一个新的FrameLayout并将其插入其中我解决了我的问题。