`
ydbc
  • 浏览: 720384 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

我的Android进阶之旅------>Android之选项卡(TabHost)的功能和用法

 
阅读更多

简介



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

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


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


step1:新建一个项目MyTabHost


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. */
	@Override
	public 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() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			//当拖动条的滑块位置发生改变时触发该方法
			@Override
			public 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() {
			@Override
			public 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:部署应用到模拟器上,查看运行效果




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

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

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

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics