2012-05-18 137 views
1

我有以下佈局如何將onClick屬性設置爲ScrollView?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/white" > 

<LinearLayout 
    android:id="@+id/answerMainFrame" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:isScrollContainer="true" 
    android:onClick="toQuestion" > 

    <ImageView 
     android:id="@+id/answer_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/question_img_cd" /> 

    <TextView 
     android:id="@+id/answer" 
     style="@style/Question" /> 
</LinearLayout> 

有時ScrollView太小,不會填滿整個屏幕,但仍想調用的方法toQuestion()任何地方點擊屏幕上。

我試過將android:layout_height="wrap_content"設置爲LinearLayoutandroid:onClick="toQuestion"ScrollView但結果相同。

+0

因此您的實際問題是如何總是有整個屏幕點擊? [這](http://stackoverflow.com/questions/5648985/ontouchlistener-for-entire-screen)可能會幫助.. – keyser

+0

我認爲可能有兩個問題,如何讓滾動視圖填充屏幕以及如何點擊時調用該方法 – FabianCook

回答

24

你可以試試讓LinearLayout實現onClick屬性,然後設置:

android:fillViewport="true" 

ScrollView

+0

這就是@Luksprog。謝謝。 – eskalera

+1

閱讀更多關於這個技巧:http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/ – saschoar

+0

完美。謝謝 –

-1

爲了使滾動視圖填滿整個屏幕的變化:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/white" > 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
android:background="@android:color/white" > 

和點擊把這個在您的onCreate方法:

((ScrollView) findViewById(R.id.scrollView)) 
       .setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         toQuestion(); 
        } 
       }); 
0

我有一個類似的問題,只是擺脫了滾動視圖本身。相反,我直接插入TextView本身,以及之前放置在ScrollView上的約束。我的ScrollView的全部目的是在TextView中滾動,而不是在幾個項目中滾動,所以它似乎是一種更優雅的方式。

添加(垂直)滾動條到我的TextView在activity.hmtl:

<TextView 
    android:id="@+id/tvInfo" 
    (...) 
    android:scrollbars="vertical" 
    /> 

一下添加到onCreate方法:

oTV = (TextView) findViewById(R.id.tvInfo); 
    oTV.setMovementMethod(new ScrollingMovementMethod()); 

無需編寫滾動響應。我喜歡。

我發現這裏的解決方案(THX Juned莫臥兒):

http://www.android-examples.com/make-textview-scrollable-in-android-programmatically/