`

【Android开发之UI】建立一个灵活的用户界面

阅读更多

 

原文自:http://android.eoe.cn/topic/ui

 

当你设计支持广泛屏幕大小的应用时,你可以在不同的布局配置中重用你的frament,在可用的屏幕空间基础上优化用户体验。
例如,在手持设备上,对于一个单窗口的用户界面来说同一时间可能只适合显示一个fragment。反之,你可能想在尺寸更大平板上并排设置更多的fragment以显示更多的信息给用户.

如上图:两个fragment,同一个activity,不同的配置,显示在不同的屏幕尺寸上。在大的屏幕中,两个fragment可以并排的占用屏幕,但是在手持设备中,在同一时间,仅仅只能填充一个fragment,两个fragment必须随着用户的操作相互替换。
为创建一个动态的体验,FragmentManager类提供了方法允许你在activity运行时对fragment进行添加,移除,和替换。

Add a Fragment to an Activity at Runtime


相比上节课提到的使用标签在布局文件中为activity定义一个fragment组件,更好的方式是在activity运行时添加fragment。如果你想在activity的生命周期中变换fragment的话就必须这样做。

执行类似添加或者删除fragment的事务,你必须使用FragmentManager创建一个FragmentTransaction,它提供了添加,删除以及其他fragment事务的API。
如果你的activity允许移除或者替换fragment,你应该在activity的onCreate())方法中添加初始化的fragment。
在你处理fragment(尤其是你在运行时添加的那些)的时候,有一个很重要的规则就是你的fragment放置位置的布局中必须有一个视图容器。
下面这个布局是上节课在同一时间只显示一个fragment布局的替代品。为了将一个fragment替换成另一个,这个activity布局包含了一个空的FrameLayout作为fragment容器。
注意这个文件名跟上节课的布局文件名字一样,但是这个布局并没有指定在“高分辨率”目录中(译者注:请注意下面xml的路径,res/layout这个layout文件夹并没有像上节课提到的是一个layout-large文件夹),如此这个布局是用在比large更小的设备屏幕上,因为这个屏幕不能在同一时间充满两个fragment。
res/layout/news_articles.xml:

1
2
3
4
    <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" />

在你的activity中,使用Support LibraryAPI,调用getSupportFragmentManager()可以得到一个FragmentManager对象,之后调用beginTransaction去创建一个FragmentTransaction对象,再调用add()方法即可添加一个fragment。
你可以对activity使用同一个FragmentTransaction对象去执行多个fragment事务,当你确定要做这些操作时,你必须调用 commit()方法。
例如,以下代码演示怎样添加一个fragment到前面的layout:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    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();
        }
    }
    }

由于fragment是在运行时添加到FrameLayout,而不是直接使用标签定义在activity的布局中,activity可以移除它或者使用另外一个不同的fragment替换它。

使用另一个fragment替换当前(Replace One Fragment with Another)* ##


替换一个fragment的过程跟添加差不多,但是需要的是replace()方法,而不是add()方法。
需要注意的是,当你执行fragment事务时,比如替换或者删除一个fragment。允许用户“后退”或者“撤销”改变通常是比较合适的做法。为了让用户可以通过fragment事务“后退”,你必须在你提交fragment事务之前调用addToBackStack()方法

注意:当你移除或者替换fragment且将事务添加到堆栈中时,被移除的fragment是被停止了(没有消亡)。如果用户导航回来重新加载这个fragment,它将会重新启动;如果你没有把事务加入到堆栈中,当fragment被删除或者替换时,这个fragment也就消亡了;

以下使用fragment替换另一个的例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    // 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 API执行更高级的fragment操作。

0
0
分享到:
评论

相关推荐

    android开发基础

    3.Android用户界面UI元素之Layout 4.Android用户界面UI元素之Widget(1) 5.Android用户界面UI元素之Widget(2) 6.Android用户界面UI元素之Widget(3) 7.Android用户界面UI元素之Widget(4) 8.Android用户界面UI元素之...

    新版Android开发教程.rar

    Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。 Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 ...

    Android高级编程--源代码

    9.1.3 建立GTalk连接,开始一个IM会话 282 9.1.4 在线状态和联系人列表简介 282 9.1.5 管理聊天会话 285 9.1.6 发送和接收数据信息 289 9.2 SMS简介 291 9.2.1 在应用程序中使用SMS 291 9.2.2 发送SMS信息 ...

    Google Android SDK开发范例大全(第3版) 1/5

    余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...

    《Google Android SDK开发范例大全(第3版)》.pdf

     《google android sdk开发范例大全(第3版)》继承前两版由浅入深的方式,范例总数由原先的160多个增加到了200多个,在用户交互界面、手机控件、交互式通信服务、手机自助服务、娱乐多媒体等方面均增加了相应的范例...

    Google Android SDK开发范例大全(第3版) 4/5

    余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...

    Google Android SDK开发范例大全(第3版) 3/5

    余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...

    Google Android SDK开发范例大全(第3版) 5/5

    余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...

    Google Android SDK开发范例大全(第3版)part2

     《Google Android SDK开发范例大全(第3版)》继承前两版由浅入深的方式,范例总数由原先的160多个增加到了200多个,在用户交互界面、手机控件、交互式通信服务、手机自助服务、娱乐多媒体等方面均增加了相应的范例...

    《Android高级编程》

    DoList资源示例 3.6.4 为不同的语言和硬件创建资源 3.6.5 运行时配置更改 3.7 深入探讨Android活动 3.7.1 创建一个活动 3.7.2 活动生命周期 3.7.3 Android活动类 3.8 小结 第4章 创建用户界面 4.1 Android UI基本...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    HybridApp 一种可以下载的Native App,其用户界面的全部或者部分元素在嵌入式浏览器组件(WebView之类的)里面运行 优雅降级 一开始就构建站点的完整功能,然后针对浏览器测试和修复。认为应该针对那些最高级、最...

    Android实验指导.doc

    利用布局安排各种控件,设计良好用户界面 "&lt;LinearLayout " "xmlns:android="schemas.android./apk/res/android" " "android:orientation="vertical" " "android:layout_width="fill_parent" " "android:layout_...

    [Android项目视频教程]通讯录的开发源码视频讲解

    Android通讯录的开发是一个复杂且精细的过程,它涉及到多个关键组件和技术的运用。在开发过程中,首先要明确通讯录的基本功能需求,如添加、编辑、删除联系人,搜索联系人,以及查看联系人的详细信息等。 为了满足...

    Android高级编程.pdf

    9.1.3 建立GTalk连接,开始一个IM会话 9.1.4 在线状态和联系人列表简介 9.1.5 管理聊天会话 9.1.6 发送和接收数据信息 9.2 SMS简介 9.2.1 在应用程序中使用SMS 9.2.2 发送SMS信息 9.2.3 监听SMS消息 9.2.4 紧急响应...

    传智播客的android开发源代码

    04_开发与运行(卸载)第一个ANDROID应用.avi 所在项目:Hello 06_电话拔号器.avi 所在项目:phone 08_短信发送器.avi 所在项目:sms 09_深入了解各种布局技术.avi 所在项目:sms & FrameLayout 10_对应用进行单元...

    Android 4游戏编程入门经典

     12.5.1 用户界面的资源  12.5.2 游戏资源  12.5.3 音效与音乐  12.6 开始编写代码  12.7 assets类  12.8 settings类  12.9 主活动  12.10 主菜单  12.11 游戏设置画面  12.12 模拟类  12.12.1 shield类 ...

Global site tag (gtag.js) - Google Analytics