Android之fragment的基本使用
来源:互联网 发布:寻侠九宫突破数据 编辑:程序博客网 时间:2024/05/21 12:40
1.在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"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fl" >
</FrameLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#c0c0c0">
<Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="btn_message"
android:text="消息"/>
<Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="btn_context"
android:text="联系人"/>
<Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="btn_find"
android:text="发现"/>
</LinearLayout>
</LinearLayout>
2.在主activity中
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
//点击按钮加入 message的 Fragment 即自定义FragmentMessage,在FragmentMessage里在加载布局
public void btn_message(View view) {
FragmentMessage=new FragmentMessage();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.fl, message);
transaction.commit();
}
public void btn_context(View view) {
FragmentContext context=new FragmentContext();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.fl, context);
transaction.commit();
}
public void btn_find(View view) {
FragmentFind find=new FragmentFind();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.fl, find);
transaction.commit();
}
}
3.自定义FragmentMessage
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentMessage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.f1, null);
return view;
}
}
4.f1布局
<?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"
android:background="#b6c6"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="消息"
android:textSize="30sp" />
</RelativeLayout>
注意:装载在主布局中必须是framelayout!!
其中,动态动态加载是固定格式。
- Android之Fragment的基本使用
- Android之fragment的基本使用
- 【Android】Android中fragment的基本使用
- fragment之基本使用
- Android Fragment基本使用
- Android Fragment基本使用
- Android中ViewPager+Fragment的基本使用
- Android中ViewPager+Fragment的基本使用
- 【Android开发】Fragment的基本使用
- Android项目ViewPager+Fragment的基本使用
- 【Android】ViewPager+Fragment的基本使用
- Fragment的基本使用
- Fragment的基本使用
- Fragment的基本使用
- 自学Android之UI组件:(二)Fragment的基本使用(上)
- 自学Android之UI组件:(三)Fragment的基本使用(中)
- 自学Android之UI组件:(四)Fragment的基本使用(下)
- android之Fragment使用
- log4net 课二
- 冒泡排序法
- WebView学习
- 使用Httpclient框架提交数据-get
- vector
- Android之fragment的基本使用
- Android中的数据储存
- 栈的压入、弹出序列
- 加密算法逆向
- C++程序变量在内存中的分配问题
- 【Oracle】7.由B-Tree算法谈Oracle的索引
- java并发编程(6)--synchronized
- HDOJ 5677 ztr loves substring(Manacher+背包型DP)
- 二路归并排序