Android视图内选项卡方法
我正在创建一个应用程序,其中每个选项卡都有不同的屏幕可供切换。最初我打算让每个选项卡显示不同的活动,但从我读到的内容来看,这似乎是一个糟糕的方法。
所以我一直在使用一个 xml 文件,其中每个屏幕的布局都在一个文件中。我使用 Java 进行编程,以显示所需的每个屏幕并隐藏我不需要的屏幕。这种方法的问题在于,它意味着我所有的 Java 都是一个文件,而我所有的 XML 都在一个文件中。有人可以向我解释一种更好的方法吗,因为我很确定一定有更好的方法。我的活动有很多屏幕,同时滚动所有这些代码会让人眼花缭乱,因为我添加的屏幕越多,代码就越大。
理想情况下,我希望每个屏幕的代码都在自己的 Java 类中,每个屏幕的布局都在自己的 xml 文件中,然后在这些文件之间进行切换。
给你我的代码示例
<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
android:windowSoftInputMode="adjustResize"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#92c223" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/tab1"
android:layout_above="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
XML CODE FOR LAYOUT GOES IN HERE. EACH NEW SCREEN IS CONTAINED WITHIN THESE LAYOUTS.
</RelativeLayout>
</ScrollView>
</LinearLayout>
<TabWidget android:layout_width="fill_parent" android:id="@android:id/tabs" android:layout_weight="0"
android:layout_gravity="bottom" android:layout_height="wrap_content"></TabWidget>
</FrameLayout>
</TabHost>
然后在我的Java代码中,我使用
if ("home".equals(tabId)) {
tab1.setVisibility(View.VISIBLE);
tab2.setVisibility(View.GONE);
tab3.setVisibility(View.GONE);
tab4.setVisibility(View.GONE);
tab5.setVisibility(View.GONE);
和 在活动中的屏幕之间切换时在选项卡之间切换,当用户单击该活动中的按钮时,我使用单击侦听器并执行此操作,
tab2.setVisibility(View.GONE);
cvv.setVisibility(View.VISIBLE);
我确信有一个更好的方法来做到这一点,如果有人能给我建议,我将不胜感激。
另一个问题是,由于它全部集中在一个活动中,每当我点击后退按钮时,它就会退出应用程序。更愿意拥有它,这样当他们回击时,它就会返回到上一个屏幕。
I am creating an application where each tab has different screens to switch between. Originally I was going to have each tab show a different activity but from what I have read, it seems that is a bad way of doing it.
So I have been using an xml file which has the layout for each screen all in the one file. And I use Java to program which to display each screen required and hide the ones I don't need. The problem I have with this approach is it means all my Java is one file, and all my XML is in one file. Could someone explain to me a better way of doing this as I am pretty sure there must be a better way. My activity has many screens and its getting an eye sore scrolling through all this code at once, as the more screens I add, the larger the code is getting.
Ideally I would like to have the code for each screen in its own Java class, and the layouts for each screen in their own xml files and just switch between these.
To give you example of my code
<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
android:windowSoftInputMode="adjustResize"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#92c223" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/tab1"
android:layout_above="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
XML CODE FOR LAYOUT GOES IN HERE. EACH NEW SCREEN IS CONTAINED WITHIN THESE LAYOUTS.
</RelativeLayout>
</ScrollView>
</LinearLayout>
<TabWidget android:layout_width="fill_parent" android:id="@android:id/tabs" android:layout_weight="0"
android:layout_gravity="bottom" android:layout_height="wrap_content"></TabWidget>
</FrameLayout>
</TabHost>
Then in my Java code, I switch between tabs using
if ("home".equals(tabId)) {
tab1.setVisibility(View.VISIBLE);
tab2.setVisibility(View.GONE);
tab3.setVisibility(View.GONE);
tab4.setVisibility(View.GONE);
tab5.setVisibility(View.GONE);
and when switching between screens within an activity, when user clicks a button in that activity, I use on click listener and do this
tab2.setVisibility(View.GONE);
cvv.setVisibility(View.VISIBLE);
I am sure there is a better way of doing this, and would be grateful if someone would give me suggestions.
Another problem is, as its all in one activity, whenever I hit the back button, it just exits the application. Would prefer to have it so when they hit back, it goes back to previous screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“更好的方法”是为每个选项卡提供自己的活动。您为什么在第一段中驳回了这种方法?这正是
TabHost
的设计目的。我通常做的是子类
TabActivity
。在其onCreate(Bundle savingInstanceState)
方法中,我对每个选项卡执行以下操作一次:然后在独立文件中开发每个
Tab#Activity
。它很好而且模块化。选项卡之间的切换由库处理。你不需要担心这一切。在选项卡中,您可以通过重写内部活动中的
onKeyDown
方法来捕获后退按钮:The 'better way' is giving each tab an activity of its own. Why did you dismiss this approach in your first paragraph? This is exactly what a
TabHost
was designed for.What I usually do is subclass
TabActivity
. In itsonCreate(Bundle savedInstanceState)
method, I do the following once for each tab:Then you develop each
Tab#Activity
in an independent file. It's nice and modular. Switching between tabs is handled by the library. You don't need to worry about all that.Within your tabs, you can capture the back button by overriding the
onKeyDown
method in your inner activity: