Android的 - Horizo​​ntalScrollView不会滚动所有的方式 [英] Android - HorizontalScrollView will not scroll all the way

查看:179
本文介绍了Android的 - Horizo​​ntalScrollView不会滚动所有的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是缠着我,而现在,进出口使用 Horizo​​ntalScrollView.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT)其中滚动滚动型我的权利,但并不是所有的方式,它是采用滚动99%百分比的权利,我可以手动滚动的方式休息,但由于某种原因,它不会做时,我称之为 fullScroll() API。

下面是一个示例code ....如果你几次按下按钮,TextView的扩大,你会看到这个问题。

活动:
    包com.example.test;

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Horizo​​ntalScrollView;
进口android.widget.TextView;
公共类MainActivity延伸活动{
    TextView的电视;
    Horizo​​ntalScrollView HSV;    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        电视=(的TextView)findViewById(R.id.textView1);
        HSV =(Horizo​​ntalScrollView)findViewById(R.id.horizo​​ntalScrollView1);
    }    公共无效btnOnClick(视图v){
        tv.append(一);
        hsv.fullScroll(Horizo​​ntalScrollView.FOCUS_RIGHT);
    }
}

布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < Horizo​​ntalScrollView
        机器人:ID =@ + ID / horizo​​ntalScrollView1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:方向=横向>            <的TextView
                机器人:ID =@ + ID / textView1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =30sp
                机器人:文字=按下按钮/>        < / LinearLayout中>
    < / Horizo​​ntalScrollView>    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        安卓的onClick =btnOnClick
        机器人:文字=按钮/>< / LinearLayout中>


解决方案

我更新了code。下面请更换您的code。这完全移动Horizo​​ntalScrollView屏幕的右侧。

下面的一个替换MainActivity.java:

 公共类AndroidHTMLActivity延伸活动{
TextView的电视;
Horizo​​ntalScrollView HSV;保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    电视=(的TextView)findViewById(R.id.textView1);}
公共无效btnOnClick(视图v){    tv.append(一);    的System.out.println(tv.getWidth()=+ tv.getWidth());
    HSV =(Horizo​​ntalScrollView)findViewById(R.id.horizo​​ntalScrollView1);
    ViewTreeObserver VTO = hsv.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
        @覆盖
        公共无效onGlobalLayout(){
            。hsv.getViewTreeObserver()removeGlobalOnLayoutListener(本);            hsv.scrollTo(tv.getWidth(),0);
            }
        });
    }}

和替换为YOUE xml文档下面的一个:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:ID =@ + ID / ll_parent_layout>
 < Horizo​​ntalScrollView
    机器人:ID =@ + ID / horizo​​ntalScrollView1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
     <的LinearLayout
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =WRAP_CONTENT
       机器人:方向=横向>
         <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:TEXTSIZE =30dp
            机器人:文字=按下按钮/>    < / LinearLayout中>
< / Horizo​​ntalScrollView><按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    安卓的onClick =btnOnClick
    机器人:文字=按钮/>< / LinearLayout中>

这code fulfile,你想要你的病情。

This is bugging me for a while now, Im using HorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) which scrolls my ScrollView to the right, but not all the way, it's takes the scrollbar 99% percent to the right, I can manually scroll the rest of the way but for some reason it does not do it when i call the fullScroll() API.

Here's a sample code.... If you push the button a few times, the TextView will expand and you'll see the problem.

Activity: package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;


public class MainActivity extends Activity {
    TextView tv;
    HorizontalScrollView hsv;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1); 
    }

    public void btnOnClick(View v) {
        tv.append("a");
        hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);       
    }
}

Layout:

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

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="push the button" />

        </LinearLayout>
    </HorizontalScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnOnClick"
        android:text="Button" />

</LinearLayout>

解决方案

I updated your code. Replace your code with below one. Which perfectly moves HorizontalScrollView to right of the screen.

Replace Your MainActivity.java with below one:

public class AndroidHTMLActivity extends Activity{
TextView tv;
HorizontalScrollView hsv;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.textView1);

}
public void btnOnClick(View v){

    tv.append("a");

    System.out.println("tv.getWidth()="+tv.getWidth());
    hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
    ViewTreeObserver vto = hsv.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
        @Override
        public void onGlobalLayout() {
            hsv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            hsv.scrollTo(tv.getWidth(), 0);
            }
        });


    }

}

And replace youe Xml file with below one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:id="@+id/ll_parent_layout">
 <HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
     <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
         <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="push the button" />

    </LinearLayout>
</HorizontalScrollView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btnOnClick"
    android:text="Button" />

</LinearLayout>

This code fulfile your condition which you wants.

这篇关于Android的 - Horizo​​ntalScrollView不会滚动所有的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆