2014-09-22 33 views
1
I want to display the tab bar and listview but when my main activity launched then my tab bar gets hided... so tell me what is the problem in my xml or my src file. 

我的問題是:我的選項卡欄未顯示,只有列表視圖顯示在全屏幕上。想要在我的發佈活動中顯示tabbar和listview

在這裏,我的lisview首先出現在後面,我的標籤欄略微可見。

Main Activity:(Here I am displaying tab host) 

    package com.example.testlist; 

    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.List; 

    import android.app.Activity; 
    import android.app.TabActivity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.res.Resources; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.TabHost; 
    import android.widget.TabHost.OnTabChangeListener; 

    public class MainActivity extends TabActivity implements OnTabChangeListener{ 


     /*******************************/ 

     //Test for tab bar 
     TabHost tabHost; 
     /*******************************/ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) 

     { 
      super.onCreate(savedInstanceState); 
      /*setContentView(R.layout.list_view_layout); 

      ListView listview=(ListView)findViewById(R.id.listView1); 
     */ 

      setContentView(R.layout.layout_tab_bar); 
      // Get TabHost Refference 
       tabHost = getTabHost(); 

       // Set TabChangeListener called when tab changed 
       tabHost.setOnTabChangedListener(this); 

       TabHost.TabSpec spec; 
       Intent intent; 

       /************* TAB1 (home)************/ 
       // Create Intents to launch an Activity for the tab (to be reused) 
       intent = new Intent().setClass(this, HomeTab.class); 
       spec = tabHost.newTabSpec("First").setIndicator("") 
          .setContent(intent); 

       //Add intent to tab 
       tabHost.addTab(spec); 

       /************* TAB2(alerts) ************/ 
       intent = new Intent().setClass(this, Tab2.class); 
       spec = tabHost.newTabSpec("Second").setIndicator("") 
          .setContent(intent); 
       tabHost.addTab(spec); 

       /************* TAB3 (newsletters)************/ 
       intent = new Intent().setClass(this, Tab3.class); 
       spec = tabHost.newTabSpec("Third").setIndicator("") 
          .setContent(intent); 
       tabHost.addTab(spec); 

       /************* TAB4(calendars) ************/ 
       intent = new Intent().setClass(this, Tab4.class); 
       spec = tabHost.newTabSpec("Fourth").setIndicator("") 
          .setContent(intent); 
       tabHost.addTab(spec); 

       /************* TAB5 (more)************/ 
       intent = new Intent().setClass(this, Tab5.class); 
       spec = tabHost.newTabSpec("Fifth").setIndicator("") 
          .setContent(intent); 
       tabHost.addTab(spec); 

       // Set drawable images to tab 
       tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.homeicon); 
       tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.alert_small_icon); 
       tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.newsl); 
       tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.menu_calendar); 
       tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.more_big); 


       // Set Tab1 as Default tab and change image 
       tabHost.getTabWidget().setCurrentTab(0); 


     } 
     @Override 
     public void onTabChanged(String tabId) { 


     } 
    } 



HomeTab.java:(this class loads the content of first tab) 

此類將加載主頁選項卡的內容,其中出現與主頁選項卡相關的內容,但我的選項卡欄隱藏在此處。

package com.example.testlist; 

    import java.util.ArrayList; 

    import android.R.anim; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.widget.ListView; 

    public class HomeTab extends Activity{ 

     MyCustomAdapter myCustomAdapter; 
     Context context=HomeTab.this; 

     String optionName[]=new String[] {"Alerts","Events","Newsletters","News","Parent Info","Logins","Parent Teacher Interviews","Flexi School LunchOrder","Contact US","Kool Content"}; 
    // String text2[]=new String[] {"sub1","sub2","sub3","sub4","sub1","sub2","sub3","sub4","sub1","sub2"}; 

     //int image[]=new int[] {R.drawable.alert_small_icon,R.drawable.events,R.drawable.newsletter,R.drawable.newsletter,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24}; 
     int imageArrow[]=new int[]{R.drawable.right_arrow}; 

     ArrayList<ListModel> mylist=new ArrayList<ListModel>(); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 

      setContentView(R.layout.list_view_layout); 

      ListView listview=(ListView)findViewById(R.id.listView1); 

      getDatainList(); 

      listview.setAdapter(new MyCustomAdapter(context, mylist)); 

      listview.setBackgroundColor(Color.WHITE); 

     } 

     private void getDatainList() 
     { 

      for(int i=0;i<10;i++) 

      { 
       ListModel li=new ListModel(); 
       li.setOptionName(optionName[i]); 
      // li.setImage(image[i]); 
       li.setImageArrow(R.drawable.arrow); 

       mylist.add(li); 
      } 

     } 




    } 

layout_tab_bar.java(This class displays the tab host and used in Main Activity) 

    <?xml version="1.0" encoding="utf-8"?> 
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <RelativeLayout 
      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" 
       android:layout_alignParentBottom="true"/> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 

     </RelativeLayout> 

    </TabHost> 


listview_layout.xml: 
This file shows how the listview is added: 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <!-- Header Starts --> 

    <RelativeLayout 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" > 

     <!-- Logo Start --> 

     <ImageView 
      android:id="@+id/menuimage" 
      android:layout_width="wrap_content" 
      android:layout_height="15dp" 
      android:src="@drawable/menuicon_big" /> 

     <TextView 
      android:id="@+id/txt_Home" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="22dp" 
      android:gravity="center" 
      android:text="HOME" 
      android:textColor="@android:color/black" 

      android:textSize="20sp" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" /> 
    </RelativeLayout> 
    <!-- Header Ends --> 

    <ImageView 
     android:id="@+id/stbernard" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:scaleType="fitXY" 
     android:src="@drawable/stbernard" /> 

    <!-- <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.01" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="10.0sp" > 
    </ListView> --> 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
    </ListView> 

</LinearLayout> 


single_row.xml: 

This file shows how the content inside listview appears for each item 




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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/rel"> 

    <ImageView 
     android:id="@+id/imageviewArr" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="21dp" 
     android:src="@drawable/arrow" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

    <View android:id="@+id/testView" 
     android:layout_height="1dp" 
     android:layout_width="match_parent" 
     android:background="@android:color/black" 
     /> 

    <TextView 
     android:id="@+id/txt_Options" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="text1" 
     android:textColor="@android:color/black" 
     android:textSize="20sp" 
     android:paddingLeft="2dp" /> 

     </RelativeLayout> 

回答

0

那是因爲你聲明content的高度,以填補這裏的家長

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

這就是爲什麼tabs正在被content重疊的,你需要的是android:layout_weight,所以你需要改變它到此

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

android:layout_weight將填滿的所有剩餘空間所以它不會互相重疊

+0

謝謝問題解決 – 2014-09-22 06:03:47

相關問題