1

在我的应用程序中,我有 2 种线性布局:一种在顶部,一种在底部。

我希望无论这些布局里面是什么,顶部的布局占屏幕高度的 60%,底部的布局占 40%。这是我的 XML 代码:

   <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1.0" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="vertical" 
         android:id="@+id/layout_top">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="0.4" 
        android:id="@+id/layout_bottom">
    </LinearLayout>

</LinearLayout>

当这些布局为空时,没有问题,它们具有良好的比例。

问题是,如果我在顶部布局中放置一个小的列表视图,例如,那么布局将采用列表视图的大小,并且不会保留 60/40% 的比例。

我希望即使我的列表视图很小(例如只有 3 个项目),布局也会保留 60%,因此在我的列表视图下放置一些空白空间。

我试图改变android:layout_heightmatch_parent但它没有改变任何东西。

4

4 回答 4

6

尝试使用这个布局它适用于我

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1.0" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="6"
        android:orientation="vertical" 
         android:id="@+id/layout_top"
         android:background="#FF0000">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_weight="4" 
        android:id="@+id/layout_bottom"
        android:background="#FF00FF">
    </LinearLayout>

</LinearLayout>

诀窍是在纵向模式下设置 alayout_height="0dip"而不是 wrap_content 而layout_with="0dip"不是在横向模式下设置 wrap_content 你可以使用 layout-land 文件夹。

于 2012-05-20T11:55:48.703 回答
1

layout_weight 指定视图布局中的额外空间。您应该先尝试测量屏幕,例如:

Display display = ((WindowManager) 
  getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int width = display.getHeight();

然后做一些像 width*0.6 和 0.4 这样的计算,这样你的布局总是有 60 40 的比例。

希望这可以帮助

于 2012-05-20T11:31:55.037 回答
0

尝试layout_height = 0dp在两个孩子LinearLayouts中。它的发生是因为你有wrap_content这可能是压倒一切的layout_weight效果。

于 2012-05-20T11:38:46.123 回答
0

只需在您的父布局中,替换android:layout_weight="1.0"android:weightSum="1.0"

这是通过设置父布局的权重和子布局的权重来实现的。孩子的体重应该等于父母的体重总和。看看这个http://blog.stylingandroid.com/archives/312

于 2012-05-20T11:52:44.800 回答