简体   繁体   English

Android:如何配置RelativeLayout以填充整个高度

[英]Android: How do I configure RelativeLayout to fill the entire height

I have a scroll view that contains a relative layout. 我有一个包含相对布局的滚动视图。 The scroll view has a gradient background and is set to fill parent. 滚动视图具有渐变背景,并设置为填充父级。 The relative layout also have a BG and i'd like it to fill the screen as well but android:layout_height="fill_parent" is not applicable for relative layout. 相对布局也有一个BG,我也希望它也能填满屏幕,但是android:layout_height="fill_parent"不适用于相对布局。

How can I configure the RelativeLayout to span the entire screen height on any screen resolution? 如何在任何屏幕分辨率下配置RelativeLayout使其跨越整个屏幕高度?

Here is my code: 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLayout"
    style="@style/mainLayoutStyle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/home_activity_bg"
        android:paddingBottom="10dp" >


    </RelativeLayout>

</ScrollView>

UPDATE: 更新:

I removed everything and left the just the ScrollView and the RelativeLayout, for some reason, on Galaxy S4 the image is cut before the end (you can see according to the blue lines that the Layout itself ends just before the end): 我删除了所有内容,只保留了ScrollView和RelativeLayout,由于某种原因,在Galaxy S4上,图像在结束之前被剪切了(您可以根据蓝线看到Layout本身在结束之前结束):

在此处输入图片说明

You have that cut because of this " android:paddingBottom="10dp" remove that line from your RelativeLayout and it will fill the screen. 由于这个“ android:paddingBottom="10dp" ,您已进行了剪切,从您的RelativeLayout删除该行,它将填满屏幕。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffff00" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff0000" >


    </RelativeLayout>

</ScrollView>

This is my code and this is my display: 这是我的代码,这是我的显示:

在此处输入图片说明

At the RelativeLayout, remove the android:paddingBottom="10dp" and make it android:layout_height="fill_parent" . 在RelativeLayout上,删除android:paddingBottom="10dp"并将其设置为android:layout_height="fill_parent"

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLayout"
    style="@style/mainLayoutStyle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/home_activity_bg" >

    </RelativeLayout>

</ScrollView>

Ended up using the following code: 最终使用以下代码:

private void setHeightToFullScreen() {
        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
        LayoutParams layoutParams = relativeLayout.getLayoutParams();
        layoutParams.height = height;
    }

please note that getHeight is deprecated and you should use getSize , but that is only supported in API level 13. 请注意,不建议使用getHeight ,而应使用getSize ,但这仅在API级别13中受支持。

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

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