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>
   
      3、在activity_main.xml中添加控件,并且在MainActivity.java调用

     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
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值