hortizontal滚动型机器人设定位置 [英] Android setting position of hortizontal scrollview

查看:217
本文介绍了hortizontal滚动型机器人设定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样它与按下按钮对应设置水平滚动型的位置。我用这个来设定失败它试图:

I am trying to set the position of the horizontal scrollview so it corresponds with the button that is pushed. I've tried using this to set it unsuccessfully:

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

这会导致什么,滚动视图不受影响。 在XML:

This results in nothing, the scrollview is unaffected. The 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>

因此​​,如果第5个按钮被按下(这是屏幕外)时启动新的活动我想设置新的视图,水平滚动视图是一路的权利与起步,一路向左。

So if the 5th button is pushed (which is offscreen) when the new activity that starts I want to set the new view so the horizontal scrollview is all the way to the right versus starting out all the way to the left.

如何设置水平滚动型的位置?

推荐答案

现在你正试图滚动到Horizo​​ntalScrollView的左上角,而不是按钮的位置。尝试滚动到按钮(X,Y)的位置是这样的:

Right now you are trying to scroll to the top-left corner of the HorizontalScrollView rather than the position of the button. Try scrolling to the (x, y) position of the button like this:

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);

编辑:

这code会不会像你期望的,如果它被放置在的onCreate()。即使你叫的setContentView(),布局尚未测量并初始化呢。这意味着 getLeft()共达()方法的都将返回0 。尝试设置滚动位置之前的布局完全初始化没有任何影响,所以你需要调用 hsv.scrollTo()的onCreate一些时​​间( )

This code will not behave as you would expect if it is placed in onCreate(). Even though you have called setContentView(), the layout has not been measured and initialized yet. This means that the getLeft() and getTop() methods will both return 0. Trying to set the scroll position before the layout is fully initialized has no effect, so you need to call hsv.scrollTo() some time after onCreate().

一种选择,似乎工作是放置code。在 onWindowFocusChanged()

One option that seems to work is placing the code in 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);
}

不过,这个函数被调用每次活动获得或失去焦点的时间,所以你可能最终往往比你预期更新的滚动位置。

However, this function is called every time the Activity gains or loses focus, so you might end up updating the scroll position more often than you intended.

有一个更优雅的解决办法是子类Horizo​​ntalScrollView并设置 onMeasure滚动位置(),你知道后视图已经被初始化。要做到这一点,我拆你的布局分为两个文件,​​并增加了一个新的类名为MyHorizo​​ntalScrollView:

A more elegant solution would be to subclass the HorizontalScrollView and set the scroll position in onMeasure(), after you know that the view has been initialized. To do this, I split your layout into two files and added a new class named MyHorizontalScrollView:

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,它会自动充气,并增加了按钮布局。然后调用超之后 onMeasure()(这样它才能知道的布局已经完成初始化),它设置滚动位置。

When MyHorizontalScrollView is created, it automatically inflates and adds the button layout. Then after calling the super onMeasure() (so that it knows the layout has finished initializing) it sets the scroll position.

这是新的main.xml中。它不仅包含了新的MyHorizo​​ntalScrollView,虽然你可以很容易地把它的线性或相对布局内并添加其他视图元素。 (您将取代 com.theisenp.test 的包,其中MyHorizo​​ntalScrollView所在的名称):

This is the new main.xml. It only contains the new MyHorizontalScrollView, though you could easily put it inside of a Linear or Relative layout and add other view elements. (You would replace com.theisenp.test with the name of the package where MyHorizontalScrollView is located):

<?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" />

这是buttons.xml布局由MyHorizo​​ntalScrollView自动充气:

And this is the buttons.xml layout that is automatically inflated by MyHorizontalScrollView:

<?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>

这篇关于hortizontal滚动型机器人设定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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