【发布时间】:2014-03-16 02:39:19
【问题描述】:
我无法找到与我在 StackOverflow 上遇到的问题类似的问题 - 基本上,我有一个带有垂直 LinerLayout 的简单 XML 布局,它使用 layout_weight 进行 10/50/40 拆分,这是我想要的(如下所示):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="@string/title"
android:textColor="@color/black"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="vertical"
android:background="@color/green">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageView
android:id="@+id/preview_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/level_preview_back"
android:focusable="false" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".4"
android:text="@string/title"
android:textColor="@color/black"
android:textStyle="bold" />
</LinearLayout>
但是 - 我希望 HorizontalScrollView 和 ImageView 被覆盖(即在另一个之上占用完全相同的空间),所以我插入了一个 RelativeLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="@string/title"
android:textColor="@color/black"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="vertical"
android:background="@color/green">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageView
android:id="@+id/preview_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/level_preview_back"
android:focusable="false" />
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".4"
android:text="@string/title"
android:textColor="@color/black"
android:textStyle="bold" />
</LinearLayout>
但是当我这样做时,layout_weight(10/50/40 拆分)会突然中断,现在包含 RelativeLayout 的 LinearLayout 突然占据了预览中的几乎所有屏幕。
有人遇到过这个问题或知道一个好的解决方法吗?我也试过直接用RelativeLayout替换LinearLayout,我也遇到了同样的问题。
编辑:进一步研究这个问题,似乎还有更多。正如我在下面评论的那样,我能够通过将 LinearLayouts 包裹在 TextViews 周围来解决这个问题,尽管我不确定为什么会这样。更新我的 SDK 和插件没有帮助(尽管我确实需要这样做....)
随着我的任务进一步推进,我遇到了第二个问题 - 我正在实现重叠的 ScrollView 和 ImageView 的第二个版本(按照 Karsten 的建议使用 FrameLayout 完成),但 layout_weight 问题又重新开始了。
测试各种安排,看来问题与我使用的图像有很大关系,分辨率相当大。用较小的图像替换它们可以解决问题 - 看起来在 ImageViews 中使用大图像或作为 LinearLayouts 的背景往往会破坏整个屏幕的布局。
编辑#2:我实际上已将 XML 加载到两个不同的设备上,并且它完全按照应有的方式呈现......这似乎是 Eclipse XML 布局查看器的错误?
(请原谅糟糕的 XML 格式 - Eclipse Android xml 编辑器在这方面令人沮丧......)
【问题讨论】:
标签: android android-layout layout android-relativelayout