您的位置:首页 > 产品设计 > UI/UE

[Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment

2013-02-03 14:59 686 查看
原文地址:https://developer.android.com/training/basics/fragments/creating.html

--------------------------------------------------------------------------------------------------------------

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
This lesson shows how to extend the
Fragment
class using the Support Library so your app remains compatible with devices running system versions as old
as Android 1.6.

你认为框架是一个activity的模块化部分,它有自己的生命周期,接受它自己的输入事件,你可以在activity运行时添加或移除这些事件(有点像“子活动”,你可以在不同的activities里重用它们)。这节课向你展示如何使用Support Library扩展Fragment类,以使你的app可以和设备运行的系统兼容,即便是像Android
1.6这样老的系统。

Note: If you decide for other reasons that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in
Fragment

class and related APIs. Just be aware that this lesson is focused on using the APIs from the Support Library, which use a specific package signature and sometimes slightly different API names than the versions included in the platform.
注意:如果你处于其他的原因决定你的app需要11或者更高级别的API level,你就不需要使用Support Library,而可以使用Fragment中安装的框架及有关的APIs。这节课仅是在关注使用Support
Library中的APIs,它们使用了一个特殊的包签名,而且与平台中版本的API名称有些许不同。

Create a Fragment Class —— 创建一个框架类

To create a fragment, extend the
Fragment
class, then override key lifecycle methods to insert your app logic, similar to the way you would with an
Activity

class.

为了创建一个框架,需扩展Fragment类,然后重写关键的生命周期方法以插入你的app逻辑,这与你对
Activity类的做法相似。


One difference when creating a
Fragment
is that you must use the
onCreateView()

callback to define the layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple fragment that specifies its own layout:

当创建一个Fragment时有一点不同,就是你必须使用onCreateView()回调方法来定义布局。事实上,这是使一个框架运行你唯一需要的回调方法。例如,下面是一个简单的框架定义了它自己的布局:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }
}


Just like an activity, a fragment should implement other lifecycle callbacks that allow you to manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's
onPause()

method is called, any fragments in the activity also receive a call to
onPause()
.

就是一个activity,一个框架应该实现其他生命周期回调函数,来允许你在向activity中添加或移除、以及在它的生命周期状态之间转换时管理它的状态。例如,当activity的onPause()方法被调用时,activity中的任何框架也将收到一个onPause()的调用。

More information about the fragment lifecycle and callback methods is available in theFragments developer guide.

更多有关框架生命周期和回调函数的信息,请见Fragments开发者指南。

Add a Fragment to an Activity using XML —— 使用XML向一个活动添加框架

While fragments are reusable, modular UI components, each instance of a
Fragment
class must be associated with a parent
FragmentActivity
.
You can achieve this association by defining each fragment within your activity layout XML file.

因为框架是可重用的、模块化的UI部件,每一个Fragment类的实例必须和父类FragmentActivity相关联。你可以通过在你的activity布局XML文件中定义每个框架来达到这种关联。

Note:
FragmentActivity
is a special activity provided in the Support Library to handle fragments on system versions
older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular
Activity
.
注意:FragmentActivity是Support
Library提供的一个特殊的activity,它控制低于API level 11的系统版本中的框架。如果你支持的最低系统版本是API level 11或者更高,那么你可以使用一个常规的Activity
Here is an example layout file that adds two fragments to an activity when the device screen is considered "large" (specified by the
large
qualifier in the directory name).

下面是一个样例布局文件,当设备屏幕被认为是“large”(在目录名称中以large限定词指定)时,它向activity中添加了两个框架。

res/layout-large/news_articles.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>


Tip: For more information about creating layouts for different screen sizes, readSupporting Different Screen Sizes.
提示:更多有关为不同屏幕大小创建布局的信息,请阅读Supporting Different Screen Sizes
Here's how an activity applies this layout:

下面展示一个activity如何应用这个布局:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);
    }
}


Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, youcannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity
when the activity first starts, as shown in the next lesson.
注意:当你通过在布局XML文件里定义来向一个activity添加一个框架时,你不可以在运行时刻移除这个框架。如果你打算在用户交互时展开或关闭你的框架,你必须在activity一开始时向其添加框架,这将会在下一节课里演示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐