【问题标题】:RelativeLayout doesn't obey parent size (with layout_weight)RelativeLayout 不服从父大小(使用 layout_weight)
【发布时间】: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>

但是 - 我希望 Horizo​​ntalScrollView 和 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


    【解决方案1】:

    您发布的 XML 在 Eclipse 预览版中对我来说很好用。您运行的是最新版本的 Android 插件吗?

    注意,可以将中间的LinearLayout和RelativeLayout替换为单个FrameLayout:

    <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:textStyle="bold" />
    
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:background="#00ff00" >
    
            <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="#ff0000"
                android:focusable="false" />
        </FrameLayout>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight=".4"
            android:text="string/title2"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    【讨论】:

    • 嗯.. 感谢您的回复。我实际上是回到这里发布我自己的答案 - 看起来如果我将 TextViews 包装在它们自己的 LinearLayouts 中并将 layout_weight 应用于 LinearLayouts 而不是直接应用于 TextViews,那么它会正确显示。我还会检查以确保我的 Eclipse 是最新的。
    • 另外,感谢有关 FrameLayout 的提示 - 你说得对,它们似乎更适合我想要实现的各种目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多