简单的Fragment切换
来源:互联网 发布:ccdd心电图数据库 编辑:程序博客网 时间:2024/04/29 16:56
package com.example.hongyan;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.LinearLayout;/** * @author HD * @date 2015-12-7 * @package_name com.example.hongyan * @file_name MainActivity.java */public class MainActivity extends Activity implements OnClickListener { private LinearLayout mTabWeixin; private LinearLayout mTabContact; private LinearLayout mTabtongxun; private LinearLayout mTabSet; private ContentFrament mWeixin; private FriendFragment mFriend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTabWeixin = (LinearLayout) findViewById(R.id.mTabWeixin); mTabContact = (LinearLayout) findViewById(R.id.mTabContact); mTabtongxun = (LinearLayout) findViewById(R.id.mTab_tongxun); setDefaultFragment(); mTabSet = (LinearLayout) findViewById(R.id.mTab_set); mTabWeixin.setOnClickListener(this); mTabContact.setOnClickListener(this); } private void setDefaultFragment() { FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); mWeixin = new ContentFrament(); transaction.replace(R.id.framLayout, mWeixin); transaction.commit(); } @Override public void onClick(View v) { // TODO 自动生成的方法存根 FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); switch (v.getId()) { case R.id.mTabWeixin: if(mWeixin == null){ mWeixin = new ContentFrament(); } transaction.replace(R.id.framLayout, mWeixin); break; case R.id.mTabContact: if( mFriend == null){ mFriend = new FriendFragment(); } transaction.replace(R.id.framLayout, mFriend); break; } transaction.commit(); }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class ContentFrament extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 自动生成的方法存根 View view = inflater.inflate(R.layout.content, container, false); return view; }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FriendFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 自动生成的方法存根 View view = inflater.inflate(R.layout.friend_fragment, container, false); return view; }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.webkit.WebView.FindListener;import android.widget.Button;import android.widget.Toast;public class TitileFragment extends Fragment { private Button mButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 自动生成的方法存根 View view = inflater.inflate(R.layout.title, container); mButton = (Button) view.findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 Toast.makeText(getActivity(), "title", Toast.LENGTH_SHORT).show(); } }); return view; }}
layout_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/titlt_fragment" android:name="com.example.hongyan.TitileFragment" android:layout_width="match_parent" android:layout_height="45dp" android:layout_alignParentTop="true" /> <include android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="55dp" android:layout_alignParentBottom="true" layout="@layout/bottombar" /> <FrameLayout android:id="@+id/framLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/bottomBar" android:layout_below="@id/titlt_fragment" > </FrameLayout></RelativeLayout>
bottombar.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="horizontal" android:background="#FF0000"> <LinearLayout android:id="@+id/mTabWeixin" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" > <Button android:id="@+id/btn_weixing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="微信" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@+id/mTabContact" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" > <Button android:id="@+id/btn_Contact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="联系人" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@+id/mTab_tongxun" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" > <Button android:id="@+id/btn_tongxun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="通讯录" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@+id/mTab_set" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" > <Button android:id="@+id/btn_set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="设置" android:visibility="gone"/> </LinearLayout></LinearLayout>
content.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="25dp" android:text="content" /></RelativeLayout>
friend_fragment.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" > <TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是微信" android:textSize="65dp" /></LinearLayout>
title.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="wrap_content" android:orientation="horizontal" android:background="#00ff00"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="button" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hongyan" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
0 0
- 简单的Fragment切换
- 简单的Fragment切换,Fragment嵌套Viewpager
- Tablayout+Fragment的简单切换
- Activity和fragment的简单切换效果
- Fragment的简单使用(切换界面)
- Android简单Fragment切换
- Fragment互相切换,点击Fragment上的按钮跳转到Fragment简单总结
- 简单讲述Android fragment切换
- Fragment的简单使用(切换界面)-FenGKun
- Fragment+ViewPager+RadioGroup实现的页面简单切换
- Fragment 实现底部导航栏的简单切换
- Fragment之间的切换
- Fragment的切换
- 关于Fragment 的切换
- 两个fragment的切换
- Fragment页面的切换
- Fragment的切换方法
- Fragment切换的演示
- C#实现简单聊天程序的方法
- 横竖切屏
- LeetCode 226:Invert Binary Tree
- DM9000网卡驱动源码分析系列00 - 初衷与目标
- 【Util】java处理日期时间 相加减大全
- 简单的Fragment切换
- Swift Up and Running——变量和常量
- 成为JavaGC专家(1)—深入浅出Java垃圾回收机制
- jstack 应用
- 让文本框textarea自动适应内容的高度
- Objective-C 编译器
- 一线互联网公司的架构实践
- vs 2013 设置website项目端口
- CrystalDiskMark硬盘检测软件及下载地址