如何将属性onClick设置为ScrollView?

时间:2012-05-18 10:53:50

标签: android 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"设置为LinearLayout,将android:onClick="toQuestion"设置为ScrollView但结果相同。

3 个答案:

答案 0 :(得分:26)

您可以尝试让LinearLayout实现onClick属性,然后设置:

android:fillViewport="true"

ScrollView

答案 1 :(得分:0)

我有一个类似的问题,只是摆脱了scrollview本身。相反,我直接插入TextView本身,使用先前放在ScrollView上的约束。我的ScrollView的整个目的是在TextView中滚动,而不是在几个项目中滚动,所以它似乎是一种更优雅的方式。

将(垂直)滚动条添加到activity.hmtl中的textview:

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

将其添加到onCreate方法:

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

无需编写滚动响应。我喜欢那样。

我在这里找到了解决方案(thx Juned Mughal):

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

答案 2 :(得分:-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();
                    }
                });