android tabbar问题

时间:2011-09-17 19:40:57

标签: android class view tabbar

在我的Android应用程序中,我有10个布局和一个带有4个按钮的标签栏。对于这4个按钮,我有4种不同的布局。在我的应用程序中,我想将标签栏放在应用程序的底部,我想在所有10个布局中显示标签栏。所以我使用view方法设计了布局。我的布局似乎如下

    <?xml version="1.0" encoding="utf-8" ?> 
    <view xmlns:android="http://schemas.android.com/apk/res/android" class="com.sample.app.TabBar$iRelativeLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<RelativeLayout android:id="@+id/TabLayout_One" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Home Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_two" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Add Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_three" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Search Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_four" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Help Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_five" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="SignIn Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_six" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="signUp Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_seven" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Profile Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_eight" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Map Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_Nine" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="ListView Screen"/> 
</RelativeLayout>

<RelativeLayout android:id="@+id/TabLayout_Ten" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="@+id/Tabs">
  <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#f5f5f5" android:text="Home Screen"/> 
</RelativeLayout>

      <view class="com.resting.spot.TabBar$iTab" android:id="@+id/Tabs" android:layout_width="fill_parent" android:layout_height="50px" android:layout_alignParentBottom="true" /> 
      </view>

上面是我的布局,在我的编码部分,我曾经将特定布局设为view.VISIBLE,其他所有布局为view.GONE,如下所示

private static final int TAB_HOME = 1;
private static final int TAB_ADD  = 2;
private static final int TAB_SCH  = 3;
private static final int TAB_HELP = 4;

private iTab            mTabs;
private RelativeLayout  mTabLayout_One;
private RelativeLayout  mTabLayout_Two;
private RelativeLayout      mTabLayout_Three;
private RelativeLayout  mTabLayout_Four;    
private RelativeLayout  mTabLayout_Five;    
private RelativeLayout  mTabLayout_Six; 
private RelativeLayout  mTabLayout_Seven;   
private RelativeLayout  mTabLayout_Eight;   
private RelativeLayout  mTabLayout_Nine;    
private RelativeLayout  mTabLayout_ten; 

@Override
public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tabbar); 

 mTabs = (iTab) this.findViewById( R.id.Tabs );
 mTabLayout_One=(RelativeLayout)findViewById(R.id.TabLayout_One);
mTabLayout_Two=(RelativeLayout)findViewById( R.id.TabLayout_two );
mTabLayout_Three = (RelativeLayout) this.findViewById( R.id.TabLayout_three );
mTabLayout_Four  = (RelativeLayout) this.findViewById( R.id.TabLayout_four );
mTabLayout_Five  = (RelativeLayout) this.findViewById( R.id.TabLayout_five );
mTabLayout_Six  = (RelativeLayout) this.findViewById( R.id.TabLayout_six);
mTabLayout_Seven  = (RelativeLayout) this.findViewById( R.id.TabLayout_seven);
mTabLayout_Eight  = (RelativeLayout) this.findViewById( R.id.TabLayout_eight);
mTabLayout_Nine  = (RelativeLayout) this.findViewById( R.id.TabLayout_nine);


mTabs.addTabMember( new TabMember( TAB_HOME, "", R.drawable.home ));
mTabs.addTabMember( new TabMember( TAB_ADD , "", R.drawable.add ) );
mTabs.addTabMember( new TabMember( TAB_SCH , "", R.drawable.search ) );
mTabs.addTabMember( new TabMember( TAB_HELP, "", R.drawable.help ) );

            mTabLayout_One.setVisibility( View.VISIBLE );
            mTabLayout_Two.setVisibility( View.GONE );
            mTabLayout_Three.setVisibility( View.GONE );
            mTabLayout_Four.setVisibility( View.GONE );                     mTabLayout_Five.setVisibility( View.GONE );
            mTabLayout_Six.setVisibility( View.GONE );
            mTabLayout_Seven.setVisibility( View.GONE );
            mTabLayout_Eight.setVisibility( View.GONE );
            mTabLayout_Nine.setVisibility( View.GONE );


            mTabs.setOnTabClickListener( new OnTabClickListener() 
            {
                public void onTabClick( int tabId )
                {
                    if( tabId == TAB_HOME )
                    {
                        mTabLayout_One.setVisibility( View.VISIBLE );
                        mTabLayout_Two.setVisibility( View.GONE );
                        mTabLayout_Three.setVisibility( View.GONE );
                        mTabLayout_Four.setVisibility( View.GONE );
mTabLayout_Five.setVisibility( View.GONE );
            mTabLayout_Six.setVisibility( View.GONE );
            mTabLayout_Seven.setVisibility( View.GONE );
            mTabLayout_Eight.setVisibility( View.GONE );
            mTabLayout_Nine.setVisibility( View.GONE );             
                    } 

                    else if( tabId == TAB_ADD )
                    {
                             mTabLayout_One.setVisibility( View.GONE );
                             mTabLayout_Two.setVisibility( View.VISIBLE );
                             mTabLayout_Three.setVisibility( View.GONE );
                             mTabLayout_Four.setVisibility( View.GONE );
mTabLayout_Five.setVisibility( View.GONE );
            mTabLayout_Six.setVisibility( View.GONE );
            mTabLayout_Seven.setVisibility( View.GONE );
            mTabLayout_Eight.setVisibility( View.GONE );
            mTabLayout_Nine.setVisibility( View.GONE );

                    }
                    else if( tabId == TAB_SCH )
                    {
                        mTabLayout_One.setVisibility( View.GONE );
                        mTabLayout_Two.setVisibility( View.GONE );
                        mTabLayout_Three.setVisibility( View.VISIBLE );
                        mTabLayout_Four.setVisibility( View.GONE );
mTabLayout_Five.setVisibility( View.GONE );
            mTabLayout_Six.setVisibility( View.GONE );
            mTabLayout_Seven.setVisibility( View.GONE );
            mTabLayout_Eight.setVisibility( View.GONE );
            mTabLayout_Nine.setVisibility( View.GONE );

                    }
                    else if( tabId == TAB_HELP )
                    {
                        mTabLayout_One.setVisibility( View.GONE );
                        mTabLayout_Two.setVisibility( View.GONE );
                        mTabLayout_Three.setVisibility( View.GONE );
                        mTabLayout_Four.setVisibility( View.VISIBLE );
mTabLayout_Five.setVisibility( View.GONE );
            mTabLayout_Six.setVisibility( View.GONE );
            mTabLayout_Seven.setVisibility( View.GONE );
            mTabLayout_Eight.setVisibility( View.GONE );
            mTabLayout_Nine.setVisibility( View.GONE );

                    }
                }
            }); 
        }
}

现在问题是每个Relativelayout我曾经有大约4个按钮和10个编辑框。在我的应用程序中,我只有一个活动类。我是否必须在单个类中声明所有这些函数,并且我必须在单个类中编写所有功能。在这种情况下,我的代码行超过了大约2000行,如果它在任何地方崩溃,我会感到很困惑。

有没有办法为所有相对布局创建一个单独的类,我可以声明按钮及其ID和所有功能,如uploadind和下载数据到服务器

或任何其他解决方案,请帮助我。我是Android和编程的新手,请帮助我,抱歉这么久的问题......

0 个答案:

没有答案