【发布时间】:2023-03-23 09:35:02
【问题描述】:
我的项目中有一个 TabHost 活动。当我有以下 XML 时,选项卡不在屏幕上,根本不显示。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activityVerticalMargin"
android:paddingLeft="@dimen/activityHorizontalMargin"
android:paddingRight="@dimen/activityHorizontalMargin"
android:paddingTop="@dimen/activityVerticalMargin"
tools:context=".SomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<some elements here>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
</TabHost>
但是,如果我稍微修改一下看起来像这样,选项卡会显示,但它们不在底部,这不是我想要的样子。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activityVerticalMargin"
android:paddingLeft="@dimen/activityHorizontalMargin"
android:paddingRight="@dimen/activityHorizontalMargin"
android:paddingTop="@dimen/activityVerticalMargin"
tools:context=".SomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<!-- layout_height above was changed to wrap_content -->
<some elements here>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
</TabHost>
为什么它在第一种情况下不起作用? layout_marginBottom 不应该使布局的高度为match_parent - margin(在这种情况下为50dp)吗?
【问题讨论】:
-
你的代码会做 match_parent - 100dp(50dp 的 tabweight 和 50dp 的 marginBottom)。 Match_parent 将占据线性布局中剩余的所有空间。在相对布局中,它将是 match_parent - 50dp(marginbottom 的 50dp)
-
那么,我应该如何改变呢?
-
将父线性布局转换为相对布局。框架布局应该有参数 align_parenttop = true 并且 tabwidget 应该有 align_parentBottom = true。让其余的留下。
-
是的,这行得通。只是好奇,有没有办法使用 LinearLayout 来完成这项工作?
-
它只是关于使用不同类型的布局和边距/填充。也会有其他方法。除非您需要使用线性布局的特定功能,否则这应该没问题。我已经提交了答案。如果您可以将其标记为正确,那就太好了
标签: android android-linearlayout android-tabhost android-framelayout tabwidget