谷歌市场项目代码详解(一)

本文详细解析谷歌市场项目的代码实现,包括如何使用DrawerLayout创建侧滑菜单,确保版本一致性。同时,介绍如何添加ActionBar以及在3.0以下版本的支持,通过PagerSlidingTabStrip实现主界面布局的左右切换。此外,文章还探讨了StateLayout的设计,用于封装不同状态的显示,并讨论了如何在Fragment中适当地加载网络数据。
摘要由CSDN通过智能技术生成


DrawerLayout

 使用v4支持库中DrawerLayout可以实现侧滑菜单版本的v4库中没有DrawerLayout个类,关联“android-support-v7-appcompat”库个库中的v4和我们项目中的v4包不是同一个版本,我们需要保持两个v4的版本致,把v7库v4复制到我们项目中即可。

修改activity_main.xml使用DrawerLayout作为根布局,并添加菜单布局如下:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
   android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 主界面 -->
    <RelativeLayout
        android:layout_width="match_parent"
    	android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </RelativeLayout>
    
    <!-- 菜单布局 -->
    <FrameLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:layout_gravity="start" />

添加ActionBar

在Android 3.0(API 11)的时候出现了ActionBarActionBar替代了统计的标题ActionBar不但可以显示标题,还可以显示应用图标,还可以增加选项菜单,还可以有导航功能

想让ActionBar3.0版本以下也能使用,可使用v7支持库中的ActionBarActivityActionBarActivity后则必须使用一个Theme.AppCompat子类主题 

时的ActionBar只有应用图标和标题,我们可以给ActionBar增加一个菜单按钮,MainActivityonCreate方法中调用initAcionBar方法,如下:

 

private void initActionBar() {
	// 获取到一个向下兼容的ActionBar
	ActionBar actionBar = getSupport
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值