Android ApiDemos之FragmentArguments
来源:互联网 发布:嘉实优化红利混合好吗 编辑:程序博客网 时间:2024/04/29 23:23
这个示例展示了如何configure 一个Fragment, 其中展示了2种方法:
1. setArguments()方法
首先 在new 一个Fragment的同时设置Argument, 之后在这个Fragment类的onCreate中取出Argument,在onCreateView中对Argument进行设置。
这里要记住Fragment的Life cycle:
onAttach --- onCreate --- onCreateView --- onActivityCreated.
2. 在view hierarchy的 inflation过程中解析出相关的attributes
以下是Fragment类说明中的一段话:
The attributes of the <fragment> tag are used to control the LayoutParams provided when attaching the fragment's view to the parent container. They can also be parsed by the fragment in onInflate(Activity, AttributeSet, Bundle) as parameters.
本例中定义了一个新的属性:android:label.
定义新属性的方法--- 在res/values/attr.xml中添加
<declare-styleable name="FragmentArguments">
<attr name="android:label" />
</declare-styleable>
之后在layout xml文件中使用这个属性,
这样就可以在onInflate()方法中把属性值取出来,在onCreateView中进行处理了。
1. setArguments()方法
首先 在new 一个Fragment的同时设置Argument, 之后在这个Fragment类的onCreate中取出Argument,在onCreateView中对Argument进行设置。
这里要记住Fragment的Life cycle:
onAttach --- onCreate --- onCreateView --- onActivityCreated.
2. 在view hierarchy的 inflation过程中解析出相关的attributes
以下是Fragment类说明中的一段话:
The attributes of the <fragment> tag are used to control the LayoutParams provided when attaching the fragment's view to the parent container. They can also be parsed by the fragment in onInflate(Activity, AttributeSet, Bundle) as parameters.
本例中定义了一个新的属性:android:label.
定义新属性的方法--- 在res/values/attr.xml中添加
<attr name="android:label" />
</declare-styleable>
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment" android:id="@+id/embedded" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:label="@string/fragment_arguments_embedded" />
这样就可以在onInflate()方法中把属性值取出来,在onCreateView中进行处理了。
/*** Parse attributes during inflation from a view hierarchy into the* arguments we handle.*/@Override public void onInflate(Activity activity, AttributeSet attrs,Bundle savedInstanceState) {super.onInflate(activity, attrs, savedInstanceState);TypedArray a = activity.obtainStyledAttributes(attrs,R.styleable.FragmentArguments);mLabel = a.getText(R.styleable.FragmentArguments_android_label);a.recycle();//不要忘记}
0 1
- Android ApiDemos之FragmentArguments
- ApiDemos--FragmentArguments的例子分析
- Android学习四之APIDemos
- Android Animation学习之 ApiDemos
- Android Dialog学习之 ApiDemos
- Android Intent学习之 ApiDemos
- Android ApiDemos详解之App_Activity_Hello World(6)
- Android ApiDemos详解之App_Activity_IntentActivityFlags(7)
- Android ApiDemos详解之App_Activity_Intents(8)
- Android ApiDemos详解之App_Activity_Persistent State(9)
- android demo之ApiDemos下的text
- [Android Samples视频系列之ApiDemos] App
- [Android Samples视频系列之ApiDemos] App
- [Android Samples视频系列之ApiDemos] App
- [Android Samples视频系列之ApiDemos] App
- [Android Samples视频系列之ApiDemos] App
- [Android Samples视频系列之ApiDemos] App
- Android ApiDemos 登顶之路(一)
- 使用Memcached、Spring AOP构建数据库前端缓存框架
- VFW
- python操作windows窗口一个小case
- 安装 Windows Service 试图加载格式不正确的程序
- gdb 调试
- Android ApiDemos之FragmentArguments
- 修改数据库文件为自动扩展以达到表空间自动扩展的目的
- touchbegan 单个手指的识别----UIView 的exclusiveTouch属性
- 驾照----有效期mark
- 快速学习使用 Windows Azure 上的 SharePoint Server 2013
- Oil Deposits (HDU 1241)
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- TBookMark(书签)
- FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT用法