繁体   English   中英

屏幕上固定比例的布局

[英]Fixed proportion of layout on screen

在我的应用程序中,我有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>

关键是要建立一个layout_height="0dip"在肖像模式和替代WRAP_CONTENT layout_with="0dip"中,你可以使用布局,土地的文件夹为风景模式,而不是WRAP_CONTENT。

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

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

然后进行一些计算,例如width * 0.6和0.4,这样您的布局将始终具有60 40的比率。

希望这可以帮助

在两个子级LinearLayouts中都尝试layout_height = 0dp 发生这种情况是因为您具有wrap_content ,它可能会覆盖layout_weight效果。

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

通过设置父布局的权重总和和子布局的权重,可以实现此目的。 孩子的体重应等于父母的体重之和。 看看这个http://blog.stylingandroid.com/archives/312

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM