官方API文档是最好的教程,从官方文档入手,同时考虑到直接访问Google比较慢,这里提供oschina的链接。
http://tool.oschina.net/apidocs/apidoc?api=android/reference
When you add a fragment as a part of your activity layout, it lives in a ViewGroup
inside the activity's view hierarchy and the fragment defines its own view layout.
LinearLayout等都继承自ViewGroup,TextView等都继承自View,ViewGroup的特点就是它可以有自己的孩子。fragment和activity、ViewGroup息息相关。
Fragment的设计原理:
Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets.
For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in the tablet layout in figure 1.
创建Fragment:
To create a fragment, you must create a subclass of Fragment
(or an existing subclass of it).
示例代码:
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
onCreateView()
View
from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
onCreateView()类似Activity的setContentView,Fragment
通过它加载自己的布局,第一次画UI时调用。
通过打气筒Inflate把一个布局转换成View对象。
Adding a fragment to an activity
方法1:Declare the fragment inside the activity's layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
name属性指定的是我们自己自己定义的Fragment。
细节,Android introduced fragments in Android 3.0 (API level 11)。使用时如果报错请在清单文件中将最小sdk改到11。
项目流程
MainActivity.java->activity_main.xml->FirstFragment.java、SecondFragment.java->firstfragment.xml、secondfragment.xml
源代码:
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.test.quan.car.firstfragment.FirstFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.test.quan.car.firstfragment.SecondFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
FirstFragment.java(SecondFragment.java类似)
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by car on 2016/8/15. 权兴权意
*/
public class FirstFragment extends android.app.Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
//通过打气筒把一个布局转换成一个View对象
View view = inflater.inflate(R.layout.firstfragment,null);
return view;
}
}
firstfragment.xml(secondfragment.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FirstFragment"
android:textColor="#ff0000"
/>
</LinearLayout>
附:
巩固下Android多媒体知识:
bmp无损,jpg、png高质量压缩。
MediaPlayer播放音频;