【问题标题】:Programmatically scroll a ScrollView to focus on a Button or a view - Android以编程方式滚动 ScrollView 以专注于 Button 或视图 - Android
【发布时间】:2013-11-15 16:33:33
【问题描述】:

我有一个有 125 个按钮的 ScrollView 的 Activity。 125 个按钮是我游戏中慢慢解锁的关卡(想想 Candy Crush Saga)。现在,如果用户在关卡上,比如说 125 中的 88,那么用户必须手动一直向下滚动到按钮 88。

我的问题是,我将如何做到这一点,以便当 Activity 加载时,它会自动向下滚动并将 ScrollView 居中于某个按钮上?

以下是我如何以编程方式创建和填充按钮的代码。

ScrollView scrollView;

@Override
public void onCreate(Bundle savedInstanceState) {


    LinearLayout lay = (LinearLayout)findViewById(R.id.linearlayout);

    for(int i = 0; i < 125; i++) {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        params.weight = 1;
        params.gravity = Gravity.CENTER;

        if(i == 0) {
            TextView textview = new TextView(this);
            textview.setLayoutParams(params);
            textview.setTextSize(15);
            textview.setText(c.getResources().getString(R.string.l1to5));

            TextView textview2 = new TextView(this);
            textview2.setLayoutParams(params);
            textview.setTextSize(15);
            textview2.setText(c.getResources().getString(R.string.goal100));

            TextView textview3 = new TextView(this);
            textview3.setLayoutParams(params);
            textview.setTextSize(15);
            textview3.setText(c.getResources().getString(R.string.catRandom));

            if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_LARGE) {
                    textview.setTextSize(22);
                    textview2.setTextSize(22);
                    textview3.setTextSize(22);
            } else if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                    textview.setTextSize(22);
                    textview2.setTextSize(22);
                    textview3.setTextSize(22);
            }

            lay.addView(textview);
            lay.addView(textview2);
            lay.addView(textview3);     

            View ruler = new View(this);
            ruler.setBackgroundColor(0xFFC2BEBF);
            lay.addView(ruler, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
        }

        if(i == 5) {

 //  ....

}



        Button button = new Button(this);
        int _id = getResources().getIdentifier("b" + (i + 1), "id", this.getPackageName());
        button.setTag(_id);
        button.setLayoutParams(params);

        if((getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 
            Configuration.SCREENLAYOUT_SIZE_LARGE) {
                button.setTextSize(22);
        } else if((getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 
            Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                button.setTextSize(22);
        }

        button.setText("Level " + (i + 1));
        final int x = i + 1;
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                singleton.setLevelJustPlayed(x);
                // ...
                } else {
                 // ...
                    Intent intent = new Intent(Levels.this, Categories.class);
                    startActivity(intent);
                }
            }
        });

        lay.addView(button);

        if(singleton.getLevel() >= (i + 1)) {
            button.setEnabled(true);
        } else {
            button.setEnabled(false);
        }
    }
// end the onCreate() method

【问题讨论】:

  • 您是否尝试过使用scrollTo method 并将关卡的坐标传递给滚动视图?

标签: java android android-studio scrollview


【解决方案1】:

试试

yourScrollView.scrollTo(0, (int) button.getY());

【讨论】:

  • 啊,好吧,这就是我要找的。不过,getY() 指的是什么?
  • getY() - 应该足以找出按钮在滚动视图中的位置
  • 它可以工作,但它不适用于smoothScrollBy => new Handler().post(new Runnable() { @Override public void run() { scrollView.smoothScrollBy(0, centerY); } });
【解决方案2】:

对于 Kotlin 用户


your_scrollview.post( { your_scrollview.scrollTo(0, your_view_inside_SV.getY().toInt()) })

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2019-05-23
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多