标签:android
利用TabHost创建标签式的版面设置,进行不同标签的切换,显示不同的背景图片。效果如下所示:
1、添加6张图片资源(直接拖入drawable文件夹),分别为gray.png, white.png, gray2.png, white2.png, gray3.png, white3.png代表的是标签默认状态和按下以后的状态。
2、在 drawable文件夹中新建一般的XML文件,picture.xml, picture2.xml, picture3.xml分别填写以下代码
picture.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/white"/> <item android:drawable="@drawable/gray"/> </selector>
picture2.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/white2"/> <item android:drawable="@drawable/gray2"/> </selector>
picture3.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/white3"/> <item android:drawable="@drawable/gray3"/> </selector>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test2.MainActivity" tools:ignore="MergeRootFrame" > <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </FrameLayout>MainActivity.xml
package com.example.test3; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.os.Build; public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main, //加载页面 tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.picture)) .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2", getResources().getDrawable(R.drawable.picture2)) .setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3", getResources().getDrawable(R.drawable.picture3)) .setContent(R.id.view3)); tabHost.setCurrentTab(1); } }4、测试以后出现这样的情况:只显示字不显示图标,则在AndroidManifest.xml进行以下更改,最终看到图标。
源代码地址: http://download.csdn.net/detail/u010499449/8311291
转载请注明出处:http://blog.csdn.net/u010499449/article/details/42213941
标签:android
原文地址:http://blog.csdn.net/u010499449/article/details/42213941