草庐IT

水平 ScrollView 的Android设置位置

coder 2023-06-09 原文

我正在尝试设置水平 ScrollView 的位置,使其与按下的按钮相对应。我试过用它来设置它不成功:

HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();
hsv.scrollTo(x, y);

这没有任何结果, ScrollView 不受影响。 xml:

 <HorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@null"
        android:scrollbars="none" >

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

            <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:text="btn0" 
                android:id="@+id/btn0"
                android:background="@drawable/yellow_btn" />

            <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:background="@drawable/yellow_btn"
                android:text="bnt1"
                android:id="@+id/btn1" />

            <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:background="@drawable/yellow_btn"
                android:text="btn2"
                android:id="@+id/btn2" />

            <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:background="@drawable/yellow_btn"
                android:text="btn3"
                android:id="@+id/btn3" />

      <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:background="@drawable/yellow_btn"
                android:text="btn4"
                android:id="@+id/btn4" />

      <Button
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="-5dp"
                android:background="@drawable/yellow_btn"
                android:text="btn5"
                android:id="@+id/btn5" />

        </LinearLayout>
    </HorizontalScrollView>

因此,如果在启动新 Activity 时按下第 5 个按钮(在屏幕外),我想设置新 View ,以便水平 ScrollView 一直向右而不是一直向左开始。

如何设置水平 ScrollView 的位置?

最佳答案

现在您正在尝试滚动到 Horizo​​ntalScrollView 的左上角,而不是按钮的位置。尝试像这样滚动到按钮的 (x, y) 位置:

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);
Button button = (Button) findViewById(R.id.btn5);
int x, y;
x = button.getLeft();
y = button.getTop();
hsv.scrollTo(x, y);

编辑:

如果将这段代码放在 onCreate() 中,它的行为将与您预期的不同。即使您调用了 setContentView(),布局还没有被测量和初始化。这意味着 getLeft()getTop() 方法 will both return 0 .尝试在布局完全初始化之前设置滚动位置没有效果,因此您需要在 onCreate() 之后的某个时间调用 hsv.scrollTo()

似乎可行的一个选项是将代码放在 onWindowFocusChanged() 中:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);
    Button button = (Button) findViewById(R.id.btn5);
    int x, y;
    x = button.getLeft();
    y = button.getTop();
    hsv.scrollTo(x, y);
}

但是,每次 Activity 获得或失去焦点时都会调用此函数,因此您最终可能会比预期更频繁地更新滚动位置。

一个更优雅的解决方案是继承 Horizo​​ntalScrollView 并在 onMeasure() 中设置滚动位置,在您知道 View 已经初始化之后。为此,我将您的布局拆分为两个文件并添加了一个名为 MyHorizo​​ntalScrollView 的新类:

package com.theisenp.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;

public class MyHorizontalScrollView extends HorizontalScrollView {

    public MyHorizontalScrollView(Context context) {
        super(context);
        addButtons(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addButtons(context);
    }

    /**
     * Inflates the layout containing the buttons and adds them to the ScrollView
     * @param context
     */
    private void addButtons(Context context) {
        View buttons = inflate(context, R.layout.buttons, null);
        addView(buttons);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //Find button 5 and scroll to its location
        View button = findViewById(R.id.btn5);
        scrollTo(button.getLeft(), button.getTop());
    }
}

创建 MyHorizo​​ntalScrollView 时,它会自动膨胀并添加按钮布局。然后在调用 super onMeasure() 之后(以便它知道布局已经完成初始化)它设置滚动位置。

这是新的 main.xml。它只包含新的 MyHorizo​​ntalScrollView,尽管您可以轻松地将它放在线性或相对布局内并添加其他 View 元素。 (您可以将 com.theisenp.test 替换为 MyHorizo​​ntalScrollView 所在包的名称):

<?xml version="1.0" encoding="utf-8"?>
<com.theisenp.test.MyHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@null"
android:scrollbars="none" />

这是由 MyHorizo​​ntalScrollView 自动膨胀的buttons.xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >

    <Button
    android:id="@+id/btn0"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="btn0" />

    <Button
    android:id="@+id/btn1"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="bnt1" />

    <Button
    android:id="@+id/btn2"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="btn2" />

    <Button
    android:id="@+id/btn3"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="btn3" />

    <Button
    android:id="@+id/btn4"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="btn4" />

    <Button
    android:id="@+id/btn5"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_marginBottom="-5dp"
    android:text="btn5" />

</LinearLayout>

关于水平 ScrollView 的Android设置位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507976/

有关水平 ScrollView 的Android设置位置的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  3. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  6. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  7. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  8. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  9. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  10. ruby - 将对象设置为 nil 是否很常见? - 2

    我正在构建一个应用程序,想知道是否将未使用的对象设置为nil是生产级编码中的常见做法。我知道这只是垃圾收集器的提示,并不总是处理对象。 最佳答案 根据这个thread如果您使用完一个成员对象,将其设置为nil将引发被引用对象被垃圾回收。如果它是局部变量,方法exit将做同样的事情。也就是说,如果您要求将成员显式设置为nil,我会质疑您的设计。 关于ruby-将对象设置为nil是否很常见?,我们在StackOverflow上找到一个类似的问题: https://

随机推荐