Android:版面配置高度(以屏幕分辨率的百分比表示) [英] Android: Layout height in percentage of screen resolution
问题描述
我在Android领域还很陌生,所以这可能是一个愚蠢的问题,但这是可行的.
I'm quite new in Android, so it'll probably be a stupid question, but here it goes.
我有一个Activity的RelativeLayout.在此布局中,我在屏幕底部有另一个布局.有时我隐藏它或使其可见,这无关紧要.是否可以使另一个布局的高度占屏幕总高度的27%?想法是保留除屏幕上的此布局以外的其他内容.
I have a RelativeLayout of Activity. In this layout I have another Layout at bottom of a screen. Sometimes I hide it or make it visible, this matters not. Is it possible to make the another layout's height lets say 27% of total screen height? The idea is to keep the other content, except this layout on screen.
有人尝试过这样的事情吗?
Have anyone tried something like this?
推荐答案
LinearLayout
可以通过android:layout_weight
处理.例如,这是一个包含三个Button
小部件的布局,分别占高度的50%,30%和20%:
LinearLayout
can handle this via android:layout_weight
. For example, here is a layout containing three Button
widgets, taking up 50%, 30%, and 20% of the height, respectively:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="50"
android:text="@string/fifty_percent"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="30"
android:text="@string/thirty_percent"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="20"
android:text="@string/twenty_percent"/>
</LinearLayout>
但是,您不能简单地声明布局文件中任意位置的任意窗口小部件应占据屏幕尺寸的任意百分比.欢迎您使用Java执行该计算,并根据需要调整小部件的LayoutParams
.
However, you cannot simply declare that an arbitrary widget at an arbitrary point in your layout file should take up an arbitrary percentage of the screen size. You are welcome to perform that calculation in Java and adjust your widget's LayoutParams
as necessary, though.
这篇关于Android:版面配置高度(以屏幕分辨率的百分比表示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!