Android TabWidget不同选项卡的不同xml文件
我想用多个选项卡实现 tabwidget,每个选项卡都代表来自单个数据源的数据,这些数据可以动态更改,所以我不能使用多个活动,因为据我了解,活动更像是一个应用程序,在隐藏时不存在。 所以我创建了这样的 tabwidget:
<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>
我像这样初始化我的应用程序
public void onCreate(Bundle savingInstanceState) { 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.gatewaylist_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);
它工作正常,但我想知道是否可以将不同选项卡的定义分离到单独的 xml 文件中。 我尝试这样做,但应用程序在尝试添加相应的选项卡时崩溃。 提前致谢。
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.gatewaylist_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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
元素? http://developer.android.com/resources/articles/layout-tricks -reuse.html这样您就可以将单个 xml 定义放在不同的文件中并将它们包含在主文件中。
Have you tried using the
<include />
element? http://developer.android.com/resources/articles/layout-tricks-reuse.htmlthis way you can put the single xml definitions in different files and include them in the main one.