简介

Android之选项卡(TabHost)的功能和用法_第1张图片" width="650" height="338" style="border:1px solid black;">


下面通过一个实例来学习TabHost,在此对上一篇

Android之拖动条(SeekBar和RatingBar)的功能和用法


使用的项目进行优化,使用TabHost使界面看起来更加友好。


step1:新建一个项目MyTabHost

Android之选项卡(TabHost)的功能和用法_第2张图片" width="239" height="424" style="border:1px solid black;">

step2:设计应用的UI界面 /layout/tabhost.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定义第一个标签页的内容 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/tab01"><ImageView android:id="@+id/image1" android:layout_width="fill_parent"android:layout_height="240px" android:src="@drawable/guitar" /><!-- 定义一个拖动条,并改变它的滑动外观 --><SeekBar android:layout_width="fill_parent"android:layout_height="wrap_content" android:max="255"android:progress="255" android:id="@+id/seekbar" android:thumb="@drawable/star" /></LinearLayout><!-- 定义第二个标签页的内容 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/tab02"><ImageView android:id="@+id/image2" android:layout_width="fill_parent"android:layout_height="240px" android:src="@drawable/wall" /><!-- 定义一个星级评分条 --><RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"android:layout_height="wrap_content" android:max="255"android:progress="255" android:numStars="5" android:stepSize="0.5" /></LinearLayout></TabHost>

step3:MyTabHostActivity.java

package cn.roco.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.RatingBar;import android.widget.SeekBar;import android.widget.TabHost;import android.widget.RatingBar.OnRatingBarChangeListener;import android.widget.SeekBar.OnSeekBarChangeListener;public class MyTabHostActivity extends TabActivity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TabHost tabHost = getTabHost();// 设置使用TabHost布局LayoutInflater.from(this).inflate(R.layout.tabhost,tabHost.getTabContentView(), true);//添加第一个标签页tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("SeekBar").setContent(R.id.tab01));//添加第二个标签页tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("RatingBar").setContent(R.id.tab02));/**使用SeekBar*/final ImageView imageView1=(ImageView) findViewById(R.id.image1);SeekBar seekBar=(SeekBar) findViewById(R.id.seekbar);seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}//当拖动条的滑块位置发生改变时触发该方法@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {//动态改变图片的透明度imageView1.setAlpha(progress);}});/**使用RatingBar*/final ImageView imageView2=(ImageView) findViewById(R.id.image2);RatingBar ratingBar=(RatingBar) findViewById(R.id.ratingbar);ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {@Overridepublic void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {//动态改变图片的透明度imageView2.setAlpha((int)(rating*255/5));}});}}

step4:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.roco.tabhost"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name="MyTabHostActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

step5:部署应用到模拟器上,查看运行效果

Android之选项卡(TabHost)的功能和用法_第3张图片" width="374" height="534" style="border:1px solid black;"> Android之选项卡(TabHost)的功能和用法_第4张图片" width="375" height="536" style="border:1px solid black;">

Android之选项卡(TabHost)的功能和用法_第5张图片" width="376" height="532" style="border:1px solid black;"> Android之选项卡(TabHost)的功能和用法_第6张图片" width="373" height="536" style="border:1px solid black;">

Android之选项卡(TabHost)的功能和用法_第7张图片" width="375" height="533" style="border:1px solid black;"> Android之选项卡(TabHost)的功能和用法_第8张图片" width="374" height="534" style="border:1px solid black;">

==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================


更多相关文章

  1. android启动界面说明
  2. 使用架包实现android异步加载图片
  3. android实现自定义相机以及图片的水印
  4. Android使用BitmapFactory.Options解决加载大图片内存溢出问题
  5. android的selector背景选择器
  6. Android中几种图像特效处理
  7. Android(安卓)Service生命周期及用法!
  8. Android应用程序资源——Drawable资源概述
  9. 浅谈android的selector背景选择器

随机推荐

  1. OpenGL Mipmapping(Android(安卓)NDK)
  2. 来电归属地数据查询Java实现
  3. Android(安卓)Dex VS Class:实例图解剖析
  4. Android学习系列(34)
  5. 谷歌称Android(安卓)3.0系统可运行在任何
  6. Android(安卓)官方:Google Play 等服务仍
  7. Android(安卓)打包及引用 aar 文件的方法
  8. Android中的Service的通信
  9. Android应用架构
  10. Android绘图系列(三)——自定义View绘制