2011-10-27 48 views
1

我很新的android開發,我一直有一些問題讓我的佈局顯示我想要的方式。 基本上我想要的是包含帶標籤圖像的四個橫向滾動視圖。直到運行時才能知道圖像的數量。我希望橫向滾動視圖可以縮放到不同的大小。Scaling ImageView裏面的Horizo​​ntalScrollView

眼下,設置我有看起來像這樣: 的main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#fff" android:id="@+id/build"> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/ahsv" /> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/ihsv" /> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_weight="2" 
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/mhsv" /> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_weight="3" 
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/rhsv" /> 

viewitem.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" android:layout_height="fill_parent" 
android:background="#fff"> 

<ImageView android:id="@+id/image" android:layout_height="fill_parent" 
    android:layout_width="fill_parent" android:scaleType="center" /> 

<TextView android:id="@+id/title" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:textColor="#000" 
    android:layout_gravity="center_horizontal|bottom" /> 

還有一個LinearLayout進入Horizo​​ntalScrollViews

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:background="#fff" 
android:id="@+id/horizll"/> 

此時我的代碼只是使用實例化這些LayoutInflator的viewitems一堆,將它們添加到的LinearLayout(horizll),這是充氣並添加到Horizo​​ntalScrollView。 (所有這些都在onCreate()中完成)

代碼幾乎按照預期工作。我有四個側面滾動列表,其高度與其權重相對應。但是,這些內部的圖像會停留在其默認大小上,換句話說,它們不會使用Horizo​​ntalScrollView的高度進行縮放。

我在做什麼錯?

在此先感謝您的幫助

回答

1

那麼我最終搞清楚了。我最終在SO上查看了更多,並在using ViewTreeObserver上發現了一個問題。設置合適的VTO後,我可以指定FrameLayout的最小寬度和高度。有點駭人聽聞,但它完成了工作。

+0

我不能讓它與最小寬度和高度一起工作,但我在ImageView上使用了setLayoutParams(),並且對我有用。 – richy

0

嘗試指定scaleType在ImageView的。例如,

android:scaleType="fitXY" 

會將小圖像拉伸到ImageView的邊界。您可能還需要指定

android:adjustViewBounds="true" 

保持寬高比。

+0

在完成您的建議後,它現在將圖像拉伸到文本的寬度。感謝您的快速回復 –

相關問題