操作栏与fragment

操作栏和frament是Android3.0新引入的界面控件,这两个控件一定程度上是为了适应Android平板电脑等大屏幕设备界面设计需要而产生的,在Android4.0系统中得到了进一步的发展,可以良好地支持不同屏幕尺寸的设备,并可以根据屏幕大小的不同改变显示内容。

操作栏

操作栏(ActionBar)代替传统的标题栏功能。操作栏左侧的图标是应用程序的图标(logo),图标旁边是应用程序当前Activity的标题,右侧的多个图标则是“选项菜单”中的菜单项。

操作栏可以提供多个实用的功能,包括:

1.将“选项菜单”的菜单项显示在操作栏的右侧

2.基于fragment实现类似于Tab页导航切换功能

3.为导航提供可“托曳放置”的下拉列表

4.可在操作栏上实现类似于“搜索框”的功能

默认情况下,所有高于Android3.0的系统中,基于holographic主题的Activity上方都存在操作栏。如果程序开发人员需要隐藏Activity的操作栏,则可以在AndroidMainfest.xml文件中添加如下代码。

<activity android:name="@android:style/Theme.Holo.NoActionBar">

或者在代码中加入:‘

ActionBar actionBar = getActionBar();

actionBar.hide();

在操作栏被隐藏后,Android系统会自动调整界面元素,填充隐藏操作栏所腾出的空间。

操作栏右侧用来显示“选项菜单”的菜单项,但所显示的内容会根据操作栏所具有的空间不同而具有不同的实现方式。在屏幕尺寸较小的设备上,操作栏会自动隐藏菜单项的文字,而仅显示菜单项的图标,而在屏幕尺寸较大的设备上,操作栏会同时显示菜单项的文字和图标。

将“选项菜单”的菜单项标识为可在操作栏中显示的代码非常简单,只需要在XML菜单资源文件的item标签中添加一行代码

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/main_memu"
        android:icon="@drawable/pic0"
        android:title="打印"
        android:showAsAction="ifRoom|withText"
        />
</menu>

 ifRoom表示如果操作栏有剩余空间,则显示该菜单项的图标;withText表示显示图标的同时显示文字标题。

分辨率的大小和屏幕的方向,一定程度上决定了操作栏的实现内容那个和实现方式。

ActionBar基本思想都是使用XML文件的菜单资源,然后再Activity中通过onCreateOptionsMenu()函数加载选项菜单,并调用onOptionsItemSelected()函数处理菜单选择事件。

在操作栏增加文字输入功能

在菜单项中添加自定义显示内容,实现的方法是在item标签中添加android:actionLayout属性,并将属性值定义为需要显示的布局文件。android:actionLayout="@layout/printView.xml"需要显示的布局文件。

fragment

fragment的主要目的在大屏幕设备上实现灵活、动态的界面设计。例如,在Android的平板电脑上,因为屏幕有更多的空间来放置更多的界面组件,并且这些组件之间还会产生一定的数据交互。

fragment支持这种设计理念,开发人员不需要管理复杂的视图结构变化,把这些动态的管理工作交给fragment和会推栈(back stack)完成。在进行界面设计时,只需要将界面布局按照功能和区域划分为不同的模块,每个模块设计成一个fragment即可。

在新闻阅读程序中,可以将界面划分为左右两个部分,并使用两个fragment实现,左侧用来展示新闻列表,右侧用来阅读新闻的具体内容。两个fragment可以并排地放置在同一个Activity中,且这两个fragment都具有自己的生命周期函数和界面输入事件。如果不使用fragment,开发人员就需要在一个Activity中是吸纳展示新闻列表,而在另一个Activity中显示新闻的具体内容。使用fragment就可以将两部分功能合并到同一个Activity中实现。


fragment被设计成为可重用的模块,每个fragment都有自己的布局和生命周期回调函数,可以将同一个fragment放置到多个不同的Activity中。为了重复使用fragment,开发人员应该避免直接从一个fragment去操纵另一个fragment,这样会增加两个fragment之间的耦合度,不利于模块的重用。

onCreate()函数是在fragment创建时被调用的,用来初始化fragment中的必要组件。onCreateView()函数是fragment在用户界面上第一次绘制时被调用的,并返回fragment的跟布局视图。onPause()函数是在用户离开fragment时被调用的,用来保存fragment中用户输入或修改的内容。


main.xml文件,两个fragment在界面上的位置关系就在这个文件中进行定义。

<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 ="edu.dalu.FragmentDemo.AFragment"
            android:id="@+id/fragment_a" 
            android:layout_weight="1"
            android:layout_width="0px" 
            android:layout_height="match_parent" />
     <fragment android:name ="edu.hrbeu.FragmentDemo.BFragment"
            android:id="@+id/fragment_b" 
            android:layout_weight="1"
            android:layout_width="0px" 
            android:layout_height="match_parent" />
</LinearLayout>
public class FragmentDemoActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
public class AFragment extends Fragment{
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
       return inflater.inflate(R.layout.frag_a, container, false);
    }
}

 frag_a.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AFragment" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是AFragment的显示区域,通过这行文字可以看到与BFragment的边界" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AF选项" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AF按钮" />
</LinearLayout>
frag_b.xml文件也是一样的。
FragmentDemoActivity是该示例主界面的Activity,加载了main.xml文件声明的界面布局。
通过main.xml文件中对fragment所在的包+类的描述,找到fragment的实现类,并调用类中的setContentView()函数绘制界面。
AFragment中只实现了onCreateView()函数,返回值是AFragment的视图。使用inflate()函数,通过制定资源文件R.layout.frag_a,获取到AFragment的视图。

本文摘自《Android应用程序开发》这本书,学习知识,写成博客,分享知识,还请原作者见谅。




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值