简体   繁体   English

屏幕上固定比例的布局

[英]Fixed proportion of layout on screen

In my app, I have 2 linear layouts: one at the top, one at the bottom. 在我的应用程序中,我有2种线性布局:一种在顶部,一种在底部。

I'd like that whatever is inside these layout , the layout of the top occupies 60% of the height of the screen and the layout of the bottom 40%. 我想无论这些布局内部什么 ,顶部的布局都占屏幕高度的60%,底部的布局占40%。 Here is my XML code: 这是我的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>

When these layouts are empty, no problem, they have the good proportion. 当这些布局为空时,没问题,它们具有良好的比例。

The problem is that if I put a small listview in the top layout for eg then the layout will take the size of the listview and won't preserve the 60/40% proportion. 问题是,如果我在顶部布局中放置一个小的列表视图,例如,那么该布局将采用列表视图的大小,而不会保留60/40%的比例。

I'd like that even if my listview is small (only 3 item for eg), the layout preserve it's 60% and so put some empty space under my listview. 我希望即使我的列表视图很小(例如,只有3个项目),布局也会保留它的60%,因此在我的列表视图下留一些空白。

I've tried to change android:layout_height to match_parent but it doesn't change anything. 我试图将android:layout_height更改为match_parent但它没有任何改变。

Try using this Layout it works for me 尝试使用此布局对我有用

<?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>

The trick is to set up a layout_height="0dip" instead of wrap_content in portrait mode and layout_with="0dip" instead of wrap_content in Landscape mode you can use layout-land folder for that. 关键是要建立一个layout_height="0dip"在肖像模式和替代WRAP_CONTENT layout_with="0dip"中,你可以使用布局,土地的文件夹为风景模式,而不是WRAP_CONTENT。

layout_weight specify extra space in the layoutfot the view. layout_weight在视图的layoutfot中指定额外的空间。 you should try measuring your screen first like: 您应该先尝试测量屏幕,例如:

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

and then doing some calculation like width*0.6 and 0.4 , this way your layout will always have 60 40 ratio. 然后进行一些计算,例如width * 0.6和0.4,这样您的布局将始终具有60 40的比率。

Hope This Helps 希望这可以帮助

try layout_height = 0dp in both the children LinearLayouts . 在两个子级LinearLayouts中都尝试layout_height = 0dp Its happening because you have wrap_content which is probably overriding the layout_weight effect. 发生这种情况是因为您具有wrap_content ,它可能会覆盖layout_weight效果。

Simply in your parent layout, replace android:layout_weight="1.0" with android:weightSum="1.0" 只需在您的父级布局中,将android:layout_weight="1.0"替换为android:weightSum="1.0"

This works by setting the weight sum of the parent layout and the weights of the children layouts. 通过设置父布局的权重总和和子布局的权重,可以实现此目的。 The weight of the children should be equal to the weight sum of the parent. 孩子的体重应等于父母的体重之和。 Take a look at this http://blog.stylingandroid.com/archives/312 看看这个http://blog.stylingandroid.com/archives/312

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

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