2012-10-07 49 views
1

我怎樣才能創建具有一個始終存在EditText是遵循以下三個規則佈局:拉伸的EditText來調整圖像

EditText觀點:

  1. 填充整個父,如果沒有圖像(在運行時,在這種情況下可見性將被設置爲View.GONE)。
  2. 如果圖像高於寬度,則填充父高度的50%。
  3. 填充父項的其餘部分,如果圖像寬度大於高度。

當然,圖像被拉伸以適應內部矩形的最底部50%,同時保持縱橫比。

enter image description here

回答

0

下面是我做到的。所得到的佈局是這樣的:

enter image description here enter image description here

enter image description here

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

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="blah" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:visibility="gone" 
     android:src="@drawable/photo1" /> 

</LinearLayout> 
0

試試這個:

<LinearLayout 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:orientation="vertical" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:ems="10" > 
</EditText> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:src="@drawable/ic_launcher"/> 
</LinearLayout> 
+0

這解決不了問題;至少不能滿足圖像被拉伸以填充父高度的底部50%的條件「 –

+0

因爲您可以獲得屏幕的高度,因此您可以隨時在運行時調整圖像大小。下面是一個調整大小的示例圖片:http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Wenhui

+0

對不起,我很感激但是這真的不能回答這個問題,請參閱[我的解決方案](http://stackoverflow.com/a/12785682/403455)。我想請你刪除這個答案來清理頁面。 ! –

0

我不相信這可以用一個單獨的XML佈局,但最簡單的方式做到這一點是僅僅有多個XML佈局文件來做到這一點您根據圖像的存在和寬高比進行充氣。