打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android Tabhost部件

android Tabhost部件

本文结合源代码和实例来说明TabHost的用法。

      使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:
 

      查看tabhost的源代码,主要实例变量有:

private TabWidget mTabWidget;
    private FrameLayout mTabContent;
    private List<TabSpec> mTabSpecs
   也就是说我们的tabhost必须有这三个东西,所以我们的.xml文件就会有规定:继续查看源代码:

if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }

 

 mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
        }
     也就是说我们的.xml文件需要TabWidget和FrameLayout标签。

接下来构建我们自己的tab实例:

      有两种方式可以实现:
      一种是继承TabActivity 类,可以使用android的自己内部定义好的.xml资源文件作容器文件。也就是在我们的代码中使用getTabHost(); , 而相应的后台源码是这样的:

this.setContentView(com.android.internal.R.layout.tab_content);
       在系统的资源文件中可以看见这个layout

 

      有了容器,然后我们就需要我们为每个tab分配内容,当然要可以是如何类型的标签:
      例如我们构建一下.xml文件
  首先tab1.xml 是一个LinearLayout布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:text="tab1 with linear layout"
        android:id="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

 
然后是tab2.xml是一个FrameLayout布局

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   
    android:id="@+id/FrameLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/LinearLayout02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:text="tab2"
                android:id="@+id/TextView01" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
       
    </FrameLayout>
接着要注册这两个FrameLayout为tabhost的Content,也就是接下来的代码:


LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
 
然后需要构建前面说的tabhost的第三个实例变量对应得内容,源代码中是这样的:

private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
 初始化是两个tab的空间然后会自动扩展:
好 我们构建我们的tabspec:

mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));   

也就是把我们的2个layout作为他的content,当然FrameLayout中可以有其他的布局,来放我的组件。
我们不需要在代码里面设置setContentView();因为getTabHost(); 这个方法调用后就已经设置了,源代码:
 

if (mTabHost == null) {
            this.setContentView(com.android.internal.R.layout.tab_content);
        }
也就是把系统的tab_content当做view设置。
运行后如下:
 
完整代码:

 TabHost mTabHost = getTabHost();
        LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));

 

 还有一种就是定义我们自己的tabhost:不用继承TabActivity

 首先建立我们自己的.xml文件,当然要包含Tabhost,TabWidget,FrameLayout,着3个标签:


<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android
    android:id="@+id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
        <TabWidget 
            android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" /> 
        <FrameLayout 
            android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
            
        </FrameLayout> 
    </LinearLayout> 
</TabHost> 

     注意的是:除了tabhost的id可以自定义外,其他的必须使用系统的id,为什么后面说,
       当然我们可以在FrameLayout里面添加view来作为tab的内容只需要在create tabspce时候添加就可以了,我们为了把每个tab的内容分开我们依然使用前面用到的两个tab xml文件
 java代码:
      获取TabHost 通过findviewbyid,

setContentView(R.layout.main);  
        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
    接下来很重要的一步是要使用TabHost.setup();
     作用是来初始化我们的TabHost容器:
    源代码是这样说的:

<p>Call setup() before adding tabs if loading TabHost using findViewById(). <i><b>However</i></b>: You do
      * not need to call setup() after getTabHost() in {@link android.app.TabActivity TabActivity}.
  也就是说通过findviewbyid,方法获得tabhost必须setup 而通过getTabHost则不用。
  setup干什么呢:源代码

mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
        if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }

mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
        }
   他主要是初始化了tabhost的两个实例变量,这里也回答了为什么我们的id必须使用系统定义的id的原因
  接下来工作就和前面相同了:

LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02));

 完整代码:

setContentView(R.layout.main);  
        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
        mTabHost.setup();
        LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02)); 

  运行结果同上。 如有问题欢迎提出。

    转载请说明出处。。。


加上源代码,有用了可以下载下:/Files/freeman1984/atab.rar

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lastsweetop/archive/2010/05/07/5566200.aspx

posted @ 2010-05-26 15:30 tt_mc 阅读(1870) | 评论(0) | 编辑

TAB放置界面底部

package com.TabTest2;
import java.lang.reflect.Field;
import android.app.Activity;
import android.os.Build;
import android.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class TabTest2 extends Activity {
    
public void onCreate(Bundle icicle) {   
        
super.onCreate(icicle);   
        setContentView(com.TabTest2.R.layout.main);   
        
final TabHost tabs=(TabHost)findViewById(com.TabTest2.R.id.tabhost);   
        
final TabWidget tabWidget=(TabWidget) findViewById(R.id.tabs);
        
int width =45;
        
int height =48;
        Field mBottomLeftStrip;
        Field mBottomRightStrip;
        tabs.setup();   
           
          
           
        tabs.addTab(tabs.newTabSpec(
"first tab")
                .setIndicator(
"news",getResources().getDrawable(com.TabTest2.R.drawable.c))
                .setContent(com.TabTest2.R.id.tab1));
        tabs.addTab(tabs.newTabSpec(
"second tab")
                .setIndicator(
"help",getResources().getDrawable(com.TabTest2.R.drawable.e))
                .setContent(com.TabTest2.R.id.tab2));
        tabs.addTab(tabs.newTabSpec(
"third tab")
                .setIndicator(
"setting",getResources().getDrawable(com.TabTest2.R.drawable.efg))
                .setContent(com.TabTest2.R.id.tab2));
           
        tabs.setCurrentTab(
0);  
        
        
        
for (int i =0; i <tabWidget.getChildCount(); i++) {
            
/**
             * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
             
*/
            tabWidget.getChildAt(i).getLayoutParams().height 
= height;
            tabWidget.getChildAt(i).getLayoutParams().width 
= width;


         
/**
          * 设置tab中标题文字的颜色,不然默认为黑色
          
*/
          
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);

          tv.setTextColor(
this.getResources().getColorStateList(android.R.color.white));




            
/**
             * 此方法是为了去掉系统默认的色白的底角
             *
             * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
             * 都是私有变量,但是我们可以通过反射来获取
             *
             * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些
             
*/
      
if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
               
try {
                  mBottomLeftStrip 
= tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
                  mBottomRightStrip 
= tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
                  
if(!mBottomLeftStrip.isAccessible()) {
                    mBottomLeftStrip.setAccessible(
true);
                  }
                  
if(!mBottomRightStrip.isAccessible()){
                    mBottomRightStrip.setAccessible(
true);
                  }
                 mBottomLeftStrip.set(tabWidget, getResources().getDrawable (com.TabTest2.R.drawable.linee));
                 mBottomRightStrip.set(tabWidget, getResources().getDrawable (com.TabTest2.R.drawable.linee));

               } 
catch (Exception e) {
                 e.printStackTrace();
               }
         } 
else {
        
         }
        View vvv 
= tabWidget.getChildAt(i);
  
if(tabs.getCurrentTab()==i){
          vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.shine));
  }
  
else {
          vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.seven));
  }

        }
        
        
/**
         * 当点击tab选项卡的时候,更改当前的背景
         
*/
        tabs.setOnTabChangedListener(
new OnTabChangeListener(){
   @Override
   
public void onTabChanged(String tabId) {
    
// TODO Auto-generated method stub
    for (int i =0; i < tabWidget.getChildCount(); i++) {
     View vvv 
= tabWidget.getChildAt(i);
     
if(tabs.getCurrentTab()==i){
             vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.shine));
     }
     
else {
             vvv.setBackgroundDrawable(getResources().getDrawable(com.TabTest2.R.drawable.seven));
     }
    }
   }});

        
    }   


}

 

 

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    
xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation
="vertical"  
    android:layout_width
="fill_parent"  
    android:layout_height
="fill_parent">  
    
<TabHost  
        
android:id="@+id/tabhost"  
        android:layout_width
="fill_parent"  
        android:layout_height
="fill_parent">  
        
<FrameLayout  
            
android:id="@android:id/tabcontent"  
            android:layout_width
="fill_parent"  
            android:layout_height
="fill_parent"  
            android:paddingBottom
="62px">  
            
<AnalogClock  
                
android:id="@+id/tab1"  
                android:layout_width
="fill_parent"  
                android:layout_height
="fill_parent"  
                android:layout_centerHorizontal
="true" />  
            
<Button  
                
android:id="@+id/tab2"  
                android:layout_width
="fill_parent"  
                android:layout_height
="fill_parent"  
                android:text
="A semi-random button" />  
        
</FrameLayout>  
        
<RelativeLayout  
            
android:layout_width="fill_parent"  
            android:layout_height
="fill_parent">  
            
<TabWidget  
                
android:id="@android:id/tabs"  
                android:layout_alignParentBottom
="true"  
                android:layout_width
="fill_parent"  
                android:layout_height
="60px" />  
        
</RelativeLayout>  
    
</TabHost>  
</LinearLayout>  

 

 

posted @ 2010-05-26 15:28 tt_mc 阅读(971) | 评论(0) | 编辑

自定义TAB

package com.testTabActivity;

import java.lang.reflect.Field;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class testTabActivity extends TabActivity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);

        
int width =45;
        
int height =48;

        
final TabHost tabs = getTabHost();
        
final TabWidget tabWidget = tabs.getTabWidget();

        Field mBottomLeftStrip;
        Field mBottomRightStrip;

        LayoutInflater.from(
this).inflate(R.layout.main, tabs.getTabContentView(), true);

        tabs.addTab(tabs.newTabSpec(
"first tab")
             .setIndicator(
"news",getResources().getDrawable(R.drawable.efg))
             .setContent(R.id.widget_layout_Blue));

        tabs.addTab(tabs.newTabSpec(
"second tab")
         .setIndicator(
"help",getResources().getDrawable(R.drawable.c))
         .setContent(R.id.widget_layout_green));

        tabs.addTab(tabs.newTabSpec(
"second tab")
             .setIndicator(
"setting",getResources().getDrawable(R.drawable.e))
             .setContent(R.id.widget_layout_red));



        
for (int i =0; i < tabWidget.getChildCount(); i++) {
            
/**
             * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
             
*/
            tabWidget.getChildAt(i).getLayoutParams().height 
= height;
            tabWidget.getChildAt(i).getLayoutParams().width 
= width;


         
/**
          * 设置tab中标题文字的颜色,不然默认为黑色
          
*/
          
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);

          tv.setTextColor(
this.getResources().getColorStateList(android.R.color.white));




            
/**
             * 此方法是为了去掉系统默认的色白的底角
             *
             * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
             * 都是私有变量,但是我们可以通过反射来获取
             *
             * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些
             
*/
      
if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
               
try {
                  mBottomLeftStrip 
= tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
                  mBottomRightStrip 
= tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
                  
if(!mBottomLeftStrip.isAccessible()) {
                    mBottomLeftStrip.setAccessible(
true);
                  }
                  
if(!mBottomRightStrip.isAccessible()){
                    mBottomRightStrip.setAccessible(
true);
                  }
                 mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));
                 mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));

               } 
catch (Exception e) {
                 e.printStackTrace();
               }
         } 
else {
        
         }
        View vvv 
= tabWidget.getChildAt(i);
  
if(tabs.getCurrentTab()==i){
          vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.shine));
  }
  
else {
          vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
  }

        }
        
/**
         * 当点击tab选项卡的时候,更改当前的背景
         
*/
        tabs.setOnTabChangedListener(
new OnTabChangeListener(){
   @Override
   
public void onTabChanged(String tabId) {
    
// TODO Auto-generated method stub
    for (int i =0; i < tabWidget.getChildCount(); i++) {
     View vvv 
= tabWidget.getChildAt(i);
     
if(tabs.getCurrentTab()==i){
             vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.shine));
     }
     
else {
             vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
     }
    }
   }});

    }

}

 

posted @ 2010-05-26 13:33 tt_mc 阅读(423) | 评论(0) | 编辑

PHP基础语法

 

类似ASP的<%,PHP可以是<?php或者是<?,结束符号是?>,当然您也可以自己指定。通常情况下,有以下几种方式:

(
1<?php…?>        //推荐使用
(2<?...?>
(
3<script language=“php” ></script>
(
4<%%>



PHP变量名的约定
:
     (
1) PHP的变量名区分大小写;
     (
2) 变量名必须以美元符号$开始;
     (
3) 变量名开头可以以下划线开始;
     (
4) 变量名不能以数字字符开头. 



单引号
          指定一个简单字符串的最简单的方法是用单引号(字符 
')括起来。 
注: 单引号字符串中出现的变量不会被变量的值替代。 

双引号
            如果用双引号(")括起字符串,PHP 懂得更多特殊字符的转义序列: 
双引号字符串最重要的一点是其中的变量名会被变量值替代。



<?php
$name = “Jane”;
print(“your name is $name”);
?>
以上脚本输出 your name is Jane
在PHP中,字符串内可以任意插入变量。
相关函数 is_set():判断变量是否设置 empty():判断变量是否为空 var_dump():判断变量类型 is函数:例如is_int(),判断变量是否是指定类型
<?php
 $a="";
 $b=0;
 $c=5;
 $d="hello";
 if(isset($a))
    echo "\$a已经定义<br>";
 else
    echo "\$a未定义<br>";

 if(empty($b))
    echo "\$b不为空<br>";
 else
    echo "\$b为空<br>";

 echo "\$c类型是:";
 var_dump($c);
 echo "<br>\$d类型是:";
 var_dump($d);

 if(is_numeric($c))
    echo "<br>\$c是整型";
 else
    echo "<br>\$c不是整型<br>";
?>

 

 


Floats
<?php$var1 = 3.14?>Floating point: <?php echo $myFloat = 3.14; ?>Round: <?php echo round($myFloat, 1); ?>Ceiling: <?php echo ceil($myFloat); ?>Floor: <?php echo floor($myFloat); ?>
输出结果:
Floating point: 3.14
Round: 3.1
Ceiling: 4
Floor: 3
 
Booleans:

<?php
   $bool1 = true;
   $bool2 = false;
   
   
   
  ?>
  $bool1: <?php echo $bool1; ?><br />
  $bool2: <?php echo $bool2; ?><br />
  <br />
  <?php
   
   $var1 = 3;
   $var2 = "cat";

   
   
   
  ?>
  $var1 is set: <?php echo isset($var1); ?><br />
  $var2 is set: <?php echo isset($var2); ?><br />
  $var3 is set: <?php echo isset($var3); ?><br />

 

输出结果:

$bool1: 1
$bool2:

$var1 is set: 1
$var2 is set: 1
$var3 is set:

 

 

Typecasting

<?php

   $var1 = "2 ";
   $var2 = $var1 + 3;
    echo $var2;
  ?>
  <br />
  <?php
   
   echo gettype($var1); echo "<br />";
   echo gettype($var2); echo "<br />";
   
   
   settype($var2, "string");
   echo gettype($var2); echo "<br />";
   
   
   $var3 = (int) $var1;
   echo gettype($var3); echo "<br />";
  ?>

输出结果:

5
string
integer
string
integer

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android中文API(114)——TabWidget
TabHost两种实现方式
Android TabHost 标签放在底部+Header
TabHost通过手势切换Activity,滑动效果
Android:自定义Tab样式
【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服