Android的碎片宣布布局,如何设置参数? [英] Android Fragment declared in layout, how to set arguments?

查看:116
本文介绍了Android的碎片宣布布局,如何设置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用getArguments()方法onCreateView,得到一些输入数据的片段。

I've got a fragment that uses getArguments() method in onCreateView, to get some input data.

我使用的是ViewPager这个片段,它工作正常。

I'm using this fragment with ViewPager and it works fine.

当我尝试在不同的活动来重用这个片段的问题开始,只显示该片段。我想片段加入到活动中的布局:

The problem starts when I try to reuse this fragment in a different activity, that shows this fragment only. I wanted to add the fragment to the activitie's layout:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment"
    android:name="com.example.ScheduleDayFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

现在的问题是:如何将一个包传递到布局声明片段

The question is: how to pass a Bundle to a fragment declared in layout?

推荐答案

最好的办法是改变到的FrameLayout,并把片段code。

The best way would be to change the to FrameLayout and put the fragment in code.

public void onCreate(...) {

    ScheduleDayFragment fragment = new ScheduleDayFragment();
    fragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction()
                .add(R.id.main_view, fragment).commit();
    ...
}

下面是布局文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

你是否担心这会以某种方式降低性能?

Are you worried this is going to reduce performance somehow?

这篇关于Android的碎片宣布布局,如何设置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆