2013-07-12 125 views
2

我正在尝试开发一个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> 
+0

http://stackoverflow.com/questions/15705832/listview-with-different-layout-inflation-for-each-row/15706073#15706073。使用列表视图并根据行类型膨胀自定义布局。 – Raghunandan

+0

这不是任何你可以在xml中真正做到的事情,你将不得不将图像调整到屏幕大小 – tyczj

回答

2

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

(请注意,这种方法没有考虑到空间的动作条会占用量。如果您正在使用一个自定义视图对措施将需要进一步的调整。)

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

按预期工作,以完全匹配我想要的东西我用'android:scaleType =“centerCrop”' – eliocs

+0

很高兴为你工作。 – Flynn81

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

+0

我希望根据设备屏幕高度将图像拉伸或压扁。所以图像总是充满设备屏幕。 – eliocs

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类像这样添加图片视图: -

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

有了这个解决方案,我想你还需要为图像视图提供一个缩放类型,如果@eliocs希望图像是全屏的话。 – Flynn81

+0

也许但只有当他想要全屏时 –

+0

我同意,如果它需要全屏。 – Flynn81

相关问题