android选项卡置于底部,Android学习 ~ 使用TabHost实现置于屏幕底部的选项卡

最近开始做一个简单的项目,要用到选项卡功能。由于是菜鸟在上网找了很久,

也找到很多种实现的方法。下面我把我觉得比较好和比较简单的方法分享给大家。

第一步先上效果图:

caa5436f38ea709b7747dc8eb02e9a02.pngfc29ab1bba13c0afd454597834d13bbe.png6b7e64b8c9fbb2790fd094a8b3b5a628.png

第二步

应用的配置文件

package="com.android.tabhost"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="21" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

MainActivity.java

package com.android.tabhost;

import android.app.TabActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TabHost;

@SuppressWarnings("deprecation")

public class MainActivity extends TabActivity {

private TabHost tabHost;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/*为当前Activity设定布局*/

setContentView(R.layout.activity_main);

/*通过id获取布局文件当中的tabHost*/

tabHost = (TabHost)findViewById(android.R.id.tabhost);

/*加入选项*/

tabHost.addTab(tabHost.newTabSpec("主页").setIndicator("Tab1").setContent(new Intent(this,tab1.class)));

tabHost.addTab(tabHost.newTabSpec("出售1").setIndicator("Tab2").setContent(new Intent(this,tab2.class)));

tabHost.addTab(tabHost.newTabSpec("出售2").setIndicator("Tab3").setContent(new Intent(this,tab3.class)));

/*设定一开始显示的选项卡*/

tabHost.setCurrentTab(0);    }

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@android:id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent"  >

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_above="@android:id/tabs"

>

android:id="@android:id/tabs"             android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"             />

第三步 简单说明

除了主Activity外,我还创建了另外的三个Activity对应于每一个选项和三个XML布局,

由于代码很简单所以就不放上来了。关键的代码我用红色标记出来了。有什么不足的或者可

以改进的可以互相交流喔~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值