Android:布局高度占屏幕分辨率的百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12302651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:22:35  来源:igfitidea点击:

Android: Layout height in percentage of screen resolution

androidlayoutheight

提问by Toochka

I'm quite new in Android, so it'll probably be a stupid question, but here it goes.

我在 Android 方面很新,所以这可能是一个愚蠢的问题,但它就在这里。

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.

我有一个RelativeLayout 的Activity。在这个布局中,我在屏幕底部有另一个布局。有时我隐藏它或让它可见,这并不重要。是否可以让另一个布局的高度达到总屏幕高度的 27%?这个想法是保留其他内容,除了屏幕上的这个布局。

Have anyone tried something like this?

有没有人尝试过这样的事情?

回答by CommonsWare

LinearLayoutcan handle this via android:layout_weight. For example, here is a layout containing three Buttonwidgets, taking up 50%, 30%, and 20% of the height, respectively:

LinearLayout可以通过android:layout_weight. 例如,这是一个包含三个Button小部件的布局,分别占据高度的 50%、30% 和 20%:

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

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 LayoutParamsas necessary, though.

但是,您不能简单地声明布局文件中任意点的任意小部件应占据屏幕大小的任意百分比。不过,欢迎您在 Java 中执行该计算并LayoutParams根据需要调整您的小部件。

回答by wSakly

you can try this, is more complexe but useful.

你可以试试这个,它更复杂但很有用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name" />

<LinearLayout
    android:id="@+id/parent_of_bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true" >

    <LinearLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="27">

       //Add your content here

   </LinearLayout>
</LinearLayout>