您的位置:首页 > 移动开发 > Android开发

Android新手入门2016(14)--FragmentTabHost实现选项卡和菜单

2016-03-26 00:17 537 查看
本文来自肥宝传说之路,引用必须注明出处!

这章憋了好久,本来想写选项卡的,学到TabHost,TabWidget的,把代码拿过来准备研究的时候,发现居然在4.0.3版本被废弃了。

百度一下,发现在后面的版本,用FragmentTabHost和LayoutInflater来代替了。网上也有一些关于Frame的内容,但是都不是入门教程的。

写得不够通俗,想直接拿代码下来研究,发现居然很多人都是上传代码片段,然后再给个收费链接。作为一个穷屌丝,只能自己一点一点去研究了。

Frament是什么?直译是片段的意思,Frament是为了解决Android同一套代码在不同尺寸屏幕的设备上显示的问题而诞生的,是Activity的一个部分。其实就是把界面分成一部分一部分的方便管理。更深入的了解可以看这里点击打开链接

FragmentActivity 继承自Activity,能够使用Frament相关内容的Activity。

FragmentTabHost 替代了TabHost的类

来,先看个图:



呃。。。MacBook的图就是打。。。

边看代码边说吧:

activity_hello_world.xml 主界面的布局文件,使用了帧布局,将菜单栏和页面发在一起,同时显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />

<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_bar">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>

</LinearLayout>


我们可以看出,菜单栏需要一个布局文件来控制这些按钮的摆放,同时,选项卡页面内,也是需要有一个布局文件。

tab_item_view.xml tab按钮的布局文件。按钮由图片ImageView和文字TextView两部分组成

<?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:gravity="center"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/bottom_bar">
</ImageView>

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:textColor="#FFFFFF">
</TextView>

</LinearLayout>


fragment1.xml 页面fragment的布局文件 C1FFC1是背景颜色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C1FFC1" >
</LinearLayout>


fragment2.xml 这里只是颜色不同而已

android:background="#EEEE00"


fragment3.xml 这里只是颜色不同而已

android:background="#FFFFFF"

fragment4.xml 这里只是颜色不同而已

android:background="#C6555D"


fragment5.xml 这里只是颜色不同而已

android:background="#000000"


Fragment1.java 就读一下布局文件

package com.fable.helloworld;

import com.fable.helloworld.R;
import com.fable.helloworld.R.layout;

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

public class Fragment1 extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment1, null);	//fragment2345其实也就是这里不同而已。
}
}


HelloWorldActivity.java 主要逻辑实现

package com.fable.helloworld;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.fable.helloworld.Fragment1;
import com.fable.helloworld.R;

public class HelloWorldActivity extends FragmentActivity {  //FragmentActivity 可以对Fragment进行操作的Activity

private FragmentTabHost mTabHost;

//布局填充器
private LayoutInflater mLayoutInflater;

//Fragment数组界面
private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
Fragment3.class, Fragment4.class, Fragment5.class };

//存放图片数组
private int mImageArray[] = { R.drawable.home,
R.drawable.ic_dialog_email, R.drawable.ic_menu_my_calendar,
R.drawable.ic_search_category_default, R.drawable.ic_input_add };

//选项卡文字
private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);

initView();//初始化视图
}

/**
* 初始化组件
*/
private void initView() {
mLayoutInflater = LayoutInflater.from(this);//布局填充,动态布局用到

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 找到TabHost选项卡
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//选项卡容器
// 得到fragment的个数
int count = mFragmentArray.length;
for (int i = 0; i < count; i++) {
// 给每个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
// 将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, mFragmentArray[i], null);//第二个参数就是选项卡对应页面的具体内容
// 设置Tab按钮的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.bottom_bar);
}
}

/**
*
* 给每个Tab按钮设置图标和文字
*/
private View getTabItemView(int index) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);//tab的动态布局
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);//设置图标
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextArray[index]);//设置文字
//这里可以看出,往一个视图view里面插东西,就是先通过id找出里面元素对象,然后在对对象进行操作
return view;
}
}


代码不多,但文件多了一点,所以上传了代码,点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: