开发者

Android TabWidget different xml files for different tabs

开发者 https://www.devze.com 2023-03-07 05:02 出处:网络
I want to implement tabwidget with several tabs, each one of them represents data from single data source which can change dynamicaly, so i cant use several activities, because as far as i understand

I want to implement tabwidget with several tabs, each one of them represents data from single data source which can change dynamicaly, so i cant use several activities, because as far as i understand Activity is more like an application which is not exists while its hidden. So ive created tabwidget like this:

        <ScrollView android:id ="@+id/settings_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
        </ScrollView>

        <ScrollView android:id="@+id/maps_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">            
        </ScrollView>

        <EditText android:id="@+id/statistics_view"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" 
                  android:gravity="center"
                  android:singleLine="false" 
                  android:layout_marginLeft="10dip" 
                  android:layout_marginRight="10dip">
        </EditText>

        <RelativeLayout android:id="@+id/about_view"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
                                  <ImageView   
                                      android:id="@+id/icon"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:tint="#55ff0000"
                                      android:src="@drawable/main"
                                      android:layout_centerInParent="true"/>      
                                  <TextView   
                                      android:id="@+id/appname"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"          
                                      android:text="@string/app_name"
                                      android:layout_below="@id/icon"
                                      android:layout_centerInParent="true"/> 
                                  <TextView   
                                      android:id="@+id/version"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"                    
                                      android:layout_below="@id/appname"
                                      android:layout_centerInParent="true"/>   
                                  <TextView   
                                      android:id="@+id/site"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"          
                                      android:text="@string/app_site"
                                      android:layout_below="@id/version"
                                      android:layout_centerInParent="true"/>    
                                 <TextView   
                                      android:id="@+id/copyright"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"          
                                      android:text="@string/app_copyright"
                                      android:layout_below="@id/site"
                                      android:layout_centerInParent="true"/>                                      
                            </RelativeLayout>

        <RelativeLayout android:id ="@+id/gatewaylist_view"
                           android:orientation="vertical"
                           android:layout_width="fill_parent"
                           android:layout_height="fill_parent"
                           android:padding="10dip"
                           >
                            <LinearLayout
                                android:id="@+id/bottom_view"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true">
                                    <!--Put whatever view item you want here -->
                                  <Button android:text="Add Gateway"
                                          android:id="@+id/BtnToClick"
                                          android:layout_width="wrap_content"
                                          android:layout_height="wrap_content"
                                         >
                                  </Button>
                           </LinearLayout>
                           <ListView
                            android:id="@+id/android:list"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:cacheColorHint="#00000000"
                            android:layout_above="@id/bottom_view"
                            />
        </RelativeLayout>

        <ScrollView android:id="@+id/gatewayparams_view"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent">
                                <RelativeLayout android:layout_width="match_parent"
                                             android:layout_height="match_parent">
                                        <LinearLayout android:id="@+id/addressLabels"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4">
                                          <TextView     android:id="@+id/lblIP"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"
                                                        android:text="@string/hostLabel"
                                                        android:layout_weight="3"/>
                                          <TextView     android:id="@+id/lblPort"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"
                                                        android:text="@string/portLabel"
                                                        android:layout_toRightOf="@id/lblIP"
                                                        android:layout_alignParentRight="true"
                                                        android:layout_weight="1"/>
                                       </LinearLayout>
                                       <LinearLayout  android:id="@+id/addressEditBoxes"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4"
                                                      android:layout_below="@id/addressLabels">
                                            <EditText android:id="@+id/edtHost"
                                                      android:layout_height="wrap_content"
                                                      android:layout_width="0dip"
                                                      android:layout_alignParentLeft="true"
                                                      android:inputType="text"
                                                      android:text="192.168.1.141"
                                                      android:layout_weight="3"/>
                                            <EditText android:id="@+id/edtPort"
                                                      android:layout_height="wrap_content"
                                                      android:layout_width="0dip"
                                                      android:inputType="number"
                                                      android:text="9988"
                                                      android:layout_alignParentRight="true"
                                                      android:layout_weight="1"
                                                      />             
                                        </LinearLayout>
                                        <CheckBox android:id="@+id/chkUseProxy"
                                                  android:layout_height="wrap_content"
                                                  android:layout_width="wrap_content"
                                                  android:layout_alignParentLeft="true"
                                                  android:text="@string/useProxyLabel"
                                                  android:layout_weight="1"
                                                  android:layout_below="@id/addressEditBoxes"/>
                                        <LinearLayout android:id="@+id/proxyLables"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4"
                                                      android:layout_below="@id/chkUseProxy">
                                          <TextView     android:id="@+id/lblProxyIP"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"
                                                        android:text="@string/hostLabel"
                                                        android:layout_weight="3"/>
                                          <TextView     android:id="@+id/lblProxyPort"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"
                                                        android:text="@string/portLabel"
                                                        android:layout_toRightOf="@id/lblProxyIP"
                                                        android:layout_alignParentRight="true"
                                                        android:layout_weight="1"/>
                                       </LinearLayout>
                                       <LinearLayout  android:id="@+id/proxyEditBoxes"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4"
                                                      android:layout_below="@id/proxyLables">
                                            <EditText   android:id="@+id/edtProxyHost"
                                                        android:layout_height="wrap_content"
                                                        android:layout_width="0dip"
                                                        android:layout_alignParentLeft="true"
                                                        android:inputType="text"
                                                        android:text="192.168.1.141"
                                                        android:layout_weight="3"/>
                                            <EditText   android:id="@+id/edtProxyPort"
                                                        android:layout_height="wrap_content"
                                                        android:layout_width="0dip"
                                                        android:inputType="number"
                                                        android:text="9988"
                                                        android:layout_alignParentRight="true"
                                                        android:layout_weight="1"
                                                      />             
                                        </LinearLayout>    
                                         <LinearLayout  android:id="@+id/encryptionLine"
                                                        android:orientation="horizontal"
                                                        android:layout_width="fill_parent"
                                                        android:layout_height="fill_parent"
                                                        android:layout_weight="4"
                                                        android:layout_below="@id/proxyEditBoxes">   
                                            <TextView     android:id="@+id/lblEncryption"
                                                          android:text="@@string/encryptiontype"
                                                          android:layout_width="0dip"
                                                          android:layout_height="wrap_content"                      
                                                          android:layout_weight ="2"/>     
                                            <Spinner      android:id="@+id/spinnerenctype"
                                                          android:layout_width="0dip"
                                                          android:layout_height="wrap_content"
                                                          android:drawSelectorOnTop="true"
                                                          android:prompt="@string/encryptiontype"
                                                          android:layout_weight ="2"
                                                      />
                                        </LinearLayout>    
                                        <LinearLayout android:id="@+id/usernameLabels"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4"
                                                      android:layout_below="@id/encryptionLine">
                                              <TextView android:id="@+id/txtUName"
                                                        android:text="@string/usernameLabel"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"                      
                                                        android:layout_weight ="2"/>
                                              <TextView android:id="@+id/txtPassword"
                                                        android:text="@string/passwordLabel"
                                                        android:layout_width="0dip"
                                                        android:layout_height="wrap_content"                          
                                                        android:layout_weight ="2"/>       
                                        </LinearLayout>
                                        <LinearLayout android:id="@+id/usernameEditBoxes"
                                                      android:orientation="horizontal"
                                                      android:layout_width="fill_parent"
                                                      android:layout_height="fill_parent"
                                                      android:layout_weight="4"
                                                      android:layout_below="@id/usernameLabels">
                                              <EditText android:id="@+id/edtUsername"
                                                        android:layout_height="wrap_content"
                                                        android:layout_width="0dip"
                                                        android:inputType="text"
                                                        android:text="devdev"
                                                        android:layout_weight="2"
                                                       />                
                                              <EditText android:id="@+id/edtPassword"
                                                        android:layout_height="wrap_content"
                                                        android:layout_width="0dip"
                                                        android:inputType="textPassword"
                                                        android:text="devdev"
                                                        android:layout_weight="2"
                                                        />
                                         </LinearLayout>
                                        <Button android:layout_height="wrap_content"
                                                android:text="@string/connect"
                                                android:id="@+id/btnConnect"
                                                android:layout_width="match_parent"
                                                android:layout_weight="1"
                                                android:layout_below="@id/usernameEditBoxes"/>
                                </RelativeLayout>
                            </ScrollView>

        <LinearLayout android:id="@+id/videofield_view"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" >
        </LinearLayout>>
   </FrameLayout>
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:layout_weight="0"/>

</LinearLayout>

and i initialize my application like this

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost


    tabHost.addTab(tabHost.newTabSpec("tab_gatewaylist").setIndicator("",res.getDrawable(R.drawable.ic_tab_favorites)).setContent(R.id.ga开发者_开发问答tewaylist_view));
    tabHost.addTab(tabHost.newTabSpec("tab_gatewayparams").setIndicator("",res.getDrawable(R.drawable.ic_tab_gatewayparams)).setContent(R.id.gatewayparams_view));
    tabHost.addTab(tabHost.newTabSpec("tab_videofield").setIndicator("",res.getDrawable(R.drawable.ic_tab_videofield)).setContent(R.id.videofield_view));
    tabHost.addTab(tabHost.newTabSpec("tab_maps").setIndicator("",res.getDrawable(R.drawable.ic_tab_maps)).setContent(R.id.maps_view));
    tabHost.addTab(tabHost.newTabSpec("tab_statistics").setIndicator("",res.getDrawable(R.drawable.ic_tab_statistics)).setContent(R.id.statistics_view));
    tabHost.addTab(tabHost.newTabSpec("tab_settings").setIndicator("",res.getDrawable(R.drawable.ic_tab_settings)).setContent(R.id.settings_view));
    tabHost.addTab(tabHost.newTabSpec("tab_about").setIndicator("",res.getDrawable(R.drawable.ic_tab_about)).setContent(R.id.about_view));


    tabHost.setCurrentTab(0);

its works ok , but i wonder if i can separate definitions fro different tabs into separate xml files. Ive tried to do this but application crashes while trying to add corresponding tab. Thanks in advance.


Have you tried using the <include /> element? http://developer.android.com/resources/articles/layout-tricks-reuse.html this way you can put the single xml definitions in different files and include them in the main one.

0

精彩评论

暂无评论...
验证码 换一张
取 消