Android之Fragment
来源:互联网 发布:mac翻墙回国的软件 编辑:程序博客网 时间:2024/06/17 01:31
Fragment是在Activity的托管下才能具有显示视图的能力;
**fragment_crime.xml**<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入标题!" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示标题"/></LinearLayout>**CrimeFragment.java**package com.duolyn.test07;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class CrimeFragment extends Fragment{ Button btn; EditText et; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //inflate是在xml中找到布局为R.layout.fragment_crime View v = inflater.inflate(R.layout.fragment_crime, container, false); et = (EditText) v.findViewById(R.id.et); btn = (Button) v.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getActivity(), et.getText().toString(), Toast.LENGTH_SHORT).show(); } }); return v; }}
**activity_crime.xml**<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" //R.id.fragmentContainer是托管Fragment的Activity的id android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="wrap_content"></FrameLayout>**CrimeActivity.java**public class CrimeActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime); //以下几行代码是使Fragment在Activity的托管下生效,即Fragment在Activity下显示视图 FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if(fragment == null){ fragment = new CrimeFragment(); fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.crime, menu); return true; }}
0 0
- android文档之Fragment
- Android昆仑镜之Fragment
- Android之Fragment
- android之Fragment
- Android学习之 Fragment
- android之Fragment
- android之Fragment
- Android之Fragment
- android之Fragment
- Android入门之Fragment
- Android之fragment初探
- Android之FragMent
- Android之Fragment通信
- Android之Fragment学习心得
- Android之Fragment优点
- android之Fragment
- Android之Fragment
- android之Fragment生命周期
- sqlite的用法
- c++ 11 多线线程系列-----------生产者消费者
- Adapter(适配器)设计模式
- mds_stores不能关闭
- 快速排序
- Android之Fragment
- WIN8/WIN8.1 任务栏经常卡死,资源管理无响应重启的解决方法
- CodeTyphon32中自带例子源码:可拖动的panel
- 黑马程序员——java反射
- Java反射
- 【最佳实践系列】一种WPF应用程序数据验证模式
- 一套完整的安卓开发推荐方案
- 几本对于笔试和面试有用的书
- python MySQLdb连接mysql失败问题