40 Android fragment (二)
2014-01-13 15:25
375 查看
myfragment.xml 文件代码 (要加载到fragment的文件)
MyFragment.java 继承 fragment
package com.example.android_fragment_add;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MyFragment extends Fragment {
public MyFragment()
{
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.myfragment, null);
Button button=(Button)view.findViewById(R.id.buttonFragment);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "show", 1).show();
}
});
return view;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
activity_main.xml 文件代码:
MainActivity.java 代码:
package com.example.android_fragment_add;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private Button button;
private FragmentManager manager;
private boolean flag=true;
//事物
FragmentTransaction transaction;
// private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
manager=this.getFragmentManager();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
transaction=manager.beginTransaction();
MyFragment myFragment=new MyFragment();
//第一个参数 是要显示的父View
transaction.add(R.id.right, myFragment);
transaction.commit();
flag=false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
![](https://img-blog.csdn.net/20140113152750218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjXzEyM19saW5iaW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<?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" > <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
MyFragment.java 继承 fragment
package com.example.android_fragment_add;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MyFragment extends Fragment {
public MyFragment()
{
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.myfragment, null);
Button button=(Button)view.findViewById(R.id.buttonFragment);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "show", 1).show();
}
});
return view;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
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="horizontal" tools:context=".MainActivity" > <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:background="#cccccc" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示" /> </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_weight="3" android:layout_width="0dp" android:layout_height="match_parent" android:background="#ccffcc" android:orientation="vertical" ></LinearLayout> </LinearLayout>
MainActivity.java 代码:
package com.example.android_fragment_add;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private Button button;
private FragmentManager manager;
private boolean flag=true;
//事物
FragmentTransaction transaction;
// private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
manager=this.getFragmentManager();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
transaction=manager.beginTransaction();
MyFragment myFragment=new MyFragment();
//第一个参数 是要显示的父View
transaction.add(R.id.right, myFragment);
transaction.commit();
flag=false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
相关文章推荐
- Android 测试工程更换被测工程
- android paddingLeft 和 layout_marginLeft的区别
- 很多朋友需求 android俄罗斯方块工程。
- Android 自动化测试
- Android 录音数据传输
- Android NDK 开发进阶之 mk文件写法
- android 控件置于屏幕最底端
- Android 通过(Parcelable)序列化对象实现Activity间传递对象
- android html布局界面
- AGPS定位基本原理浅析
- javah命令提示无法访问android.app.activity类文件
- android学习(一)结构,布局与生命周期
- Android上传文件到服务器--带进度条
- android 源码 for nexus serial pad
- Android list刷新后仍然定位到原来的位置,解决。
- Android sqlitedatabase 事务
- Android sqlitedatabase 事务
- android Training Stopping and Restarting an Activity
- Android NDK开发环境搭建及案例
- android布局属性之weight