簡體   English   中英

我無法滾動我的自定義視圖

[英]I can't scroll my customized view

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

    <ScrollView android:id="@+id/scroll"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" android:fillViewport="true">

        <adam.music.testfont.NwcDrawingArea android:id="@+id/NwcDrawingArea"
              android:layout_height="2000dp"
              android:layout_width="2000dp"
           />
           </ScrollView>
   </LinearLayout>

我創建了 Android 項目並如上所述編輯了 main.xml。 然后我自定義視圖來繪制圖像。 要查看滾動自定義視圖的效果,我如上所述設置了寬度和高度。 現在我可以在自定義視圖中看到圖像,但它不會滾動。 你能幫我解決什么問題嗎? 我應該添加更多內容嗎?

ScrollView 與其內容之間存在相互作用。 例如,如果您將這個 TextView 插入到 ScrollView 中,而不是 NwcDrawingArea 小部件:

<TextView android:text="8This is a lot of text 7This is a lot of text 6This is a lot of text 5This is a lot of text 4This is a lot of text 3This is a lot of text 2This is a lot of text 1This is a lot of text"
android:layout_width="10dp"
android:layout_height="2000dp"
android:background="#FF00FF00"
/>

您將看到一個細長的綠色垂直 TextView,其文本比屏幕長,並且 ScrollView 顯示滾動條,讓您可以看到 TextView 的隱藏部分到文本的范圍 現在,更改 TextView layout_width="2000dp"。 TextView 變成一個全屏綠色區域,其中有一行文本從屏幕右側延伸。 當然,ScrollView 不顯示水平滾動條。 然而,ScrollView 也沒有顯示垂直滾動條,即使我們將 TextView 的大小設置為比屏幕長得多。 ScrollView 試圖確定其內容中視覺上有趣的部分,因此您需要了解您要子類化的任何小部件的行為。

例如,ScrollView 按照您期望它與 TextView 的行為方式尊重 LinearLayout 和 RelativeLayout 的布局大小。 事實上,通常將 LinearLayout 或 RelativeLayout ——而不是像 TextView 這樣的視圖——作為 ScrollView 的子級。 將 TextView 放入固定高度的 LinearLayout 中,您應該會得到預期的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM