-
12-12-2019 - |
题
在我的应用程序中,我有 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_height
到 match_parent
但这并没有改变任何事情。
解决方案
尝试使用此布局它适用于我
<?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>
.
诀窍是在纵向模式和生成模式下的wrap_content而不是wrap_content在横向模式下设置一个layout_height="0dip"
,而是可以使用Layout-Land文件夹。
其他提示
layout_weight在layoutfot中指定额外的视图。您应该尝试首先测量屏幕:
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getHeight();
.
然后做一些计算宽度* 0.6和0.4,这种方式将始终具有60个40比率。
希望这有助于
尝试 layout_height = 0dp
在两个孩子身上 LinearLayouts
. 。它的发生是因为你有 wrap_content
这可能会覆盖 layout_weight
影响。
只是在您的父布局中,用android:layout_weight="1.0"
替换生成的android:weightSum="1.0"
这是通过设置父布局的权重和子布局的权重。儿童的重量应该等于父母的重量和。看看这个 http://blog.stylingandandroid.com/archives/312