2013-07-12 120 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

相關問題