39 Android fragment 简单使用
2014-01-13 14:26
537 查看
HelloFragment.java代码
hello.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" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_main.xml 文件代码 引用fragment
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#cccccc" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.android_fragment_helloworld.HelloFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
MainActivity.java 代码
package com.example.android_fragment_helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
}
HelloFragment.java代码
package com.example.android_fragment_helloworld; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HelloFragment extends Fragment { public HelloFragment() { } //实例化一些变量 @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.hello, null); return view; } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); } }
hello.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" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_main.xml 文件代码 引用fragment
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#cccccc" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.android_fragment_helloworld.HelloFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
MainActivity.java 代码
package com.example.android_fragment_helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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菜鸟学习笔记27----Fragment的简单使用
- Android中DialogFragment的简单使用及常见问题
- Android的DialogFragment的简单使用。
- android 中Fragment 的简单使用
- Android Fragment 片段 的简单使用
- android Fragment 简单的使用
- Android之---FragmentTabHost使用简单介绍
- android FragmentTabHost 的简单使用
- Android 实现同个Activity中存在多个Fragment多次切换之后依次返回(一)(Fragment回退栈简单使用)
- Android使用Fragment应用实战,简单的实习例子
- Android零基础入门第85节:Fragment使用起来非常简单
- Fragment+ViewPager的简单使用 Android
- android学习之fragment的简单使用
- android FragmentTabHost的简单使用
- Android零基础入门第85节:Fragment使用起来非常简单
- Android fragment的简单使用使用和通信
- android fragment的简单使用
- android Fragment的简单使用
- android中fragment简单使用以及fragment之间数据交互
- android FragmentTabHost 的简单使用