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

Posted

技术标签:

【中文标题】Android:布局高度占屏幕分辨率的百分比【英文标题】:Android: Layout height in percentage of screen resolution 【发布时间】:2012-08-31 10:12:08 【问题描述】:

我是 android 的新手,所以这可能是一个愚蠢的问题,但它就是这样。

我有一个 Activity 的 RelativeLayout。在这个布局中,我在屏幕底部有另一个布局。有时我将其隐藏或使其可见,这无关紧要。是否可以让另一个布局的高度占屏幕总高度的 27%?这个想法是保留其他内容,除了屏幕上的这个布局。

有人试过这样的吗?

【问题讨论】:

您的意思是说您想要屏幕的两个部分 - 顶部占屏幕的 73%,第二部分占屏幕的 27%?或者您的意思是您想要一个位于另一个视图内的视图,在可见时覆盖整个父级底部的 27%? 【参考方案1】:

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

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

<Button
    android:id="@+id/button1"
    android:layout_
    android:layout_
    android:text="@string/app_name" />

<LinearLayout
    android:id="@+id/parent_of_bottom_layout"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:layout_alignParentBottom="true" >

    <LinearLayout
        android:id="@+id/content_view"
        android:layout_
        android:layout_
        android:layout_weight="27">

       //Add your content here

   </LinearLayout>
</LinearLayout>

【讨论】:

你能详细说明它是如何工作的以及它为什么回答这个问题吗? 你不能在 RelativeLayout 中使用 android:layout_weight 所以,我使用 LinearLayout 在相对布局中按比例制作重量,我从我的代码项目中找到了解决方案,所以我分享了它。 谢谢,这是解决问题的有趣方法。 没什么,是给安卓朋友的。【参考方案2】:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <Button
        android:layout_
        android:layout_
        android:layout_weight="50"
        android:text="@string/fifty_percent"/>

    <Button
        android:layout_
        android:layout_
        android:layout_weight="30"
        android:text="@string/thirty_percent"/>

    <Button
        android:layout_
        android:layout_
        android:layout_weight="20"
        android:text="@string/twenty_percent"/>

</LinearLayout>

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

【讨论】:

以上是关于Android:布局高度占屏幕分辨率的百分比的主要内容,如果未能解决你的问题,请参考以下文章

如何在嵌套线性布局中使 imageButton 的高度占屏幕尺寸的 25%。 Android XML 横向布局

div css布局网页如何实现网页自动适应屏幕高度和宽度

Android 屏幕适配方案(转载)

安卓屏幕适配

几种页面布局方式的简单说明

适配布局类型