Android Fragment 应用
来源:互联网 发布:vb怎么随机生成数字 编辑:程序博客网 时间:2024/06/07 03:47
1、使用Fragment 可以方便的替代TabActivity、ViewGroup
2、同时也省去了在AndroidManifest.xml文件中 添加相应的Activity
3、Fragment 是3.0之后的功能,如果想向下兼容我们在导包时一定要注意了,该导入 import android.support.v4.app.FragmentActivity; 而不是 import android.app.Fragment;
4、同时向下兼容我们的Activity 要继承 FragmentActivity 而不是 Activity
5、核心代码:
package com.example.testdialog;import android.annotation.SuppressLint;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;@SuppressLint("NewApi")public class MainActivity extends FragmentActivity implements OnClickListener { private TextView tab1 = null; private TextView tab2 = null; private FragmentManager fm = null; private FragmentTransaction ft = null;
private LinearLayout content = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(); fm = getSupportFragmentManager(); } private void findViewById() {
this.content = (LinearLayout)this.findViewById(R.id.content); this.tab1 = (TextView) this.findViewById(R.id.tab1); this.tab2 = (TextView) this.findViewById(R.id.tab2); this.tab1.setOnClickListener(this); this.tab2.setOnClickListener(this); } @Override public void onClick(View v) { ft = fm.beginTransaction(); switch (v.getId()) { case R.id.tab1: ft.replace(R.id.content, new MyFragment1()); break; case R.id.tab2: ft.replace(R.id.content, new MyFragment2()); break; default: break; } ft.commit(); }}
activity_main.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tab1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Tab1" /> <TextView android:id="@+id/tab2" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Tab2" /> <TextView android:id="@+id/tab3" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Tab3" /> <TextView android:id="@+id/tab4" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Tab2" /> </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20dip" android:orientation="horizontal" > </LinearLayout></LinearLayout>
package com.example.testdialog;import android.annotation.SuppressLint;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;@SuppressLint("NewApi")public class MyFragment1 extends Fragment { private Button button1 = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, null); button1 = (Button) view.findViewById(R.id.button1); return view; }}
6、这样我们就可以在相应的 Fragment 中做我们的UI设计了
- Android Fragment 应用
- Android Fragment应用实战
- Android Fragment应用实战
- Android Fragment应用实战
- android fragment生命周期应用
- Android Fragment应用实战
- Android应用开发:Fragment
- Android Fragment 全面详解+Fragment应用实例
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- Android中Fragment的应用
- php无法用mail函数发送邮件之原因
- 关于java技术不得不说的密码
- jquery之显示多个文本框剩余输入字数
- 透过表象看本质!?之二——除了最小p乘,还有PCA
- css hack(ie6-9,firefox,chrome,opera,safari)
- Android Fragment 应用
- IOS5 StoryBoard 入门1
- HDU 1999不可摸数(筛选因子)
- ZigBee Light Link协议灯控方案
- 程序员面试宝典第二版87页勘误
- Oracle11g 新的后台进程的功能
- 推荐几本书,给产品经理同学
- 什么最重要?
- pcre_exec()详解