[英]android layout - position ImageButtons in equal quadrants of the screen and size them to proportion
[英]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.