简介
下面通过一个实例来学习TabHost,在此对上一篇
使用的项目进行优化,使用TabHost使界面看起来更加友好。
step1:新建一个项目MyTabHost
step2:设计应用的UI界面 /layout/tabhost.xml
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
step5:部署应用到模拟器上,查看运行效果
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:
==================================================================================================