android布局中关于Tabhost的一点小陷阱!

来源:互联网 发布:linux查看hba卡驱动 编辑:程序博客网 时间:2024/05/16 09:28

今天要用一个tabhost,几下子就完成了布局,正常的结果应该如下图:

可结果确是下图的布局:

Tabhost的标签栏完全没有显示出来。被第一个tag的布局所覆盖。

而且布局代码也看出来什么问题。

我看了很长时间才找出来为什么,注意标红色的那两句。

<错误代码>如下:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="
http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="360dip"
           >
           <TabHost xmlns:android="
http://schemas.android.com/apk/res/android
            android:id="@+id/KPtabhost" 
            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"> 
                         <LinearLayout
                            xmlns:android="
http://schemas.android.com/apk/res/android"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:id="@+id/tabfirst">
                             <Button
                               android:id="@+id/KPtakePic"
                               android:layout_height="60dip"
                               android:layout_width="80dip"
                               android:text="拍摄"
                                />
                              <Button
                               android:id="@+id/KPclearPic"
                               android:layout_toRightOf="@id/KPtakePic"
                               android:layout_marginLeft="165dip"
                               android:layout_alignParentRight="true"
                               android:layout_height="60dip"
                               android:layout_width="80dip"
                               android:text="清除"
                                />
                        </LinearLayout>
   
                        <LinearLayout
                          xmlns:android="
http://schemas.android.com/apk/res/android"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:id="@+id/tabsecond" 
                          >
                             <EditText
                                 android:id="@+id/KPinforEdit"
                                 android:layout_width="fill_parent"
                                 android:layout_height="200dip"
                             />
                        </LinearLayout>
                    </FrameLayout> 
              
  </TabWidget>
             </LinearLayout> 
        </TabHost>
     </LinearLayout>

大家有没有意识到什么问题。我这样写TabWidget的话,相当于把TabWidget当成了一个layout一样的东西,

和FrameLayout里的布局是归属关系,而不是平行关系。所以正确的代码应该是:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="
http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="360dip"
           >
           <TabHost xmlns:android="
http://schemas.android.com/apk/res/android
            android:id="@+id/KPtabhost" 
            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"> 
                         <LinearLayout
                            xmlns:android="
http://schemas.android.com/apk/res/android"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:id="@+id/tabfirst">
                             <Button
                               android:id="@+id/KPtakePic"
                               android:layout_height="60dip"
                               android:layout_width="80dip"
                               android:text="拍摄"
                                />
                              <Button
                               android:id="@+id/KPclearPic"
                               android:layout_toRightOf="@id/KPtakePic"
                               android:layout_marginLeft="165dip"
                               android:layout_alignParentRight="true"
                               android:layout_height="60dip"
                               android:layout_width="80dip"
                               android:text="清除"
                                />
                        </LinearLayout>
   
                        <LinearLayout
                          xmlns:android="
http://schemas.android.com/apk/res/android"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:id="@+id/tabsecond" 
                          >
                             <EditText
                                 android:id="@+id/KPinforEdit"
                                 android:layout_width="fill_parent"
                                 android:layout_height="200dip"
                             />
                        </LinearLayout>
                    </FrameLayout> 

                //删除掉
             </LinearLayout> 
        </TabHost>
     </LinearLayout>

这样子布局就正确了。希望大家以后不要犯这样的小错误,很耽误时间!