如何将布局元素高度定义为屏幕高度

时间:2013-07-12 16:37:22

标签: android android-layout

我正在尝试开发一个Android布局,其中我有Imagesome text

Desired layout

我希望图片 1个屏幕高,向下滚动时会显示其余内容。

我应该在layout.xml中设置什么值。所以图像完全(设备无关)1屏幕高?

为了使问题更清晰,使用css我可以轻松获得following code的全高屏幕图像:

<html>
    <style>
    html, body {
        height: 100%;
    }

    .image {
        height: 100%;
        background-image: url(http://kolobee.com/images/stings/baelo-claudia.1ovl/ad2p/620x620.jpg);
        background-position: center;
    }
    </style>
    <body>
        <div class="image"></div>
        <h1>Title</h1>
        <p>More text</p>
    </body>
</html>

3 个答案:

答案 0 :(得分:2)

您可以使用覆盖ImageView类的onMeasure()方法的自定义视图来实现此目的。下面是自定义视图,以及与您问题中的图像匹配的布局。

(注意,此方法没有考虑ActionBar将占用的空间量。如果您使用的是自定义视图的度量,则需要进一步调整。)

package com.example.testview;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

public class FullScreenImageView extends ImageView {

    private DisplayMetrics mMetrics;

    public FullScreenImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMetrics = new DisplayMetrics();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        WindowManager windowManager = (WindowManager) ((Activity)getContext()).getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(mMetrics);
        setMeasuredDimension(mMetrics.widthPixels, mMetrics.heightPixels);
    }
}

布局xml将类似于:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

       <com.example.testview.FullScreenImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"                
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <Text
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text" />
    </LinearLayout>

</ScrollView>

答案 1 :(得分:1)

您可以使用以下方法动态添加图像视图并将其高度设置为屏幕高度: 如果您想要显示尺寸(以像素为单位),可以使用getSize:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

如果您不在活动中,可以通过WINDOW_SERVICE获取默认显示:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated 

但是要做到这一点,你必须从java类添加Image View,如下所示: -

ImageView iv = new ImageView();
parentLayout.Add(iv);

答案 2 :(得分:-2)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <ImageView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</ScrollView>

相关问题