Android:创建动态UI

来源:互联网 发布:淘宝美工招聘信息 编辑:程序博客网 时间:2024/04/29 00:17

创建动态的UI

       当你需要的你的APP支持很广泛的屏幕大小时,你能够基于有限的屏幕空间,在不同的布局配置中重复使用片段来提高用户体验。

       例如:在手机上面,可能一次只能显示一个片段,但是在平板上面,你可能需要并排显示几个片段。

       FragmentManager类提供了在活动运行时添加,移除和替换片段的方法,这样就能够创建动态的用户体验。

 

在运行时添加一个片段到活动中来

       跟在布局文件中定义片段不同的是,你可以在活动运行时添加一个片段,同时,你也可以在活动的生命周期内改变这个片段。

       要对片段进行添加或者移除操作,你必须使用FragmentManager来创建一个FragmentTransaction,它提供了添加,删除,替换以及其它的片段APIS 的操作。

       如果你的活动允许删除和替换片段,你应该在活动的onCreate()方法中添加初始的片段。

       在对片段进行操作是,尤其是在活动运行时添加一个片段操作,有一个很重要的原则,那就是片段必须在布局中拥有一个View容器,片段的布局就生成在这个容器中。

       如下是一个可替换片段的布局,它只有一个片段。为了保证可替换性,活动的布局包含了一个空的FrameLayout作为片段的容器。

       注意文件名跟上篇文章的一样,但是布局文件目录没有large限定语。因为这个布局是用于小屏的,不能同时显示两个片段。

res/layout/news_articles.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/fragment_container"

    android:layout_width="match_parent"

android:layout_height="match_parent" />

 

在活动里面,调用getSupportFragmentManager()来得到FragmentManager来使用Support Library APIs.然后调用beginTransaction()来创建一个FragmentTransaction,最后调用add()来添加一个片段。

用同一个FragmentTransaction,你能够执行多个片段操作。当你确认做出改变时,必须调用commit()来提交。

例子如下:

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);

 

        // Check that the activity is using the layout version with

        // the fragment_container FrameLayout

        if (findViewById(R.id.fragment_container) != null) {

 

           // However, if we're being restored from a previous state,

           // then we don't need to do anything and should return or else

           // we could end up with overlapping fragments.

           if (savedInstanceState != null) {

               return;

           }

 

           // Create an instance of ExampleFragment

           HeadlinesFragment firstFragment = new HeadlinesFragment();

           

           // In case this activity was started with special instructions from an Intent,

            // pass the Intent's extras to the fragment as arguments

           firstFragment.setArguments(getIntent().getExtras());

           

           // Add the fragment to the 'fragment_container' FrameLayout

           getSupportFragmentManager().beginTransaction()

                   .add(R.id.fragment_container, firstFragment).commit();

        }

    }

}

              因为这个片段是在活动运行时添加的,所以你可以替换或者移除它。

 

片段替换

              替换一个片段的程序跟添加片段类似,不同的是使用replace()方法。

              紧记:当你进行片段交易时,应该允许用户导航回来以及撤销操作。要保证能够导航回来,在你提交改变之前必须调用addToBackStack()

注意:当你移除或者替换一个片段并添加到后台堆栈中时,这个片段是处于stop状态(不是destory)。用户返回时,它restart.如果不添加到后台堆栈,那么被移除的片段就销毁了。

例子:

// Create fragment and give it an argument specifying the article it should show

ArticleFragment newFragment = new ArticleFragment();

Bundle args = new Bundle();

args.putInt(ArticleFragment.ARG_POSITION, position);

newFragment.setArguments(args);

 

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

 

// Replace whatever is in the fragment_container view with this fragment,

// and add the transaction to the back stack so the user can navigate back

transaction.replace(R.id.fragment_container, newFragment);

transaction.addToBackStack(null);

 

// Commit the transaction

transaction.commit();

 

addToBackStack()方法有一个可选的字符串参数来为这个片段交易指定一个名字。除非你要通过FragmentManager.BackStackEntry APIs.

进行一些高级的片段操作,否则这个名字是不需要的。

原创粉丝点击