Android LinearLayout中的碎片会更改其高度

Android LinearLayout中的碎片会更改其高度,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我试图在android应用程序中用XML设置非常简单的东西 我想把片段放在每个活动的顶部,并尝试将屏幕划分为2/8权重(总和10)。但不幸的是,这种情况正在不断地发生变化。不知道为什么。有什么帮助吗?:) 编辑2: 谢谢你们。有趣的是,我已经尝试过这个0dip的东西,但它不起作用。现在它开始工作了,但对于任何其他遇到类似问题的人来说,这里有一个提示:不要使用 android:layout_width="fill_parent" 在我的示例代码中,在这些东西内部的任何东西上。始终使用 andro

我试图在android应用程序中用XML设置非常简单的东西

我想把片段放在每个活动的顶部,并尝试将屏幕划分为2/8权重(总和10)。但不幸的是,这种情况正在不断地发生变化。不知道为什么。有什么帮助吗?:)

编辑2: 谢谢你们。有趣的是,我已经尝试过这个0dip的东西,但它不起作用。现在它开始工作了,但对于任何其他遇到类似问题的人来说,这里有一个提示:不要使用

android:layout_width="fill_parent"
在我的示例代码中,在这些东西内部的任何东西上。始终使用

android:layout_width="match_parent"

这就是0dip溶液不起作用的原因。非常感谢。

如果您想通过权重控制视图的高度,请尝试将片段和滚动视图的高度从
wrap\u content
更改为
match\u parent
0dip


 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="top" >

    <fragment
            android:id="@+id/fragment_top"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="2"
            android:layout_gravity="top"
            class="com.app.TopFragment" />

    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="8"
        android:id="@+id/chart_scrollview" >   

   <!-- Here goes LinearLayout with other linearlayouts inside-->

    </ScrollView>
    </LinearLayout>

那么您应该在xml中编写类似的内容

<com.app.TopFragment
    android:id="@+id/fragment_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="top"
    class="com.app.TopFragment" />

片段是像
myFragment扩展片段那样的自定义类吗?
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="top" >

    <fragment
            android:id="@+id/fragment_top"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="2"
            android:layout_gravity="top"
            class="com.app.TopFragment" />

    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="8"
        android:id="@+id/chart_scrollview" >   

   <!-- Here goes LinearLayout with other linearlayouts inside-->

    </ScrollView>
    </LinearLayout>
<com.app.TopFragment
    android:id="@+id/fragment_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="top"
    class="com.app.TopFragment" />