java - 以编程方式滚动 ScrollView 以关注按钮或 View - Android

标签 java android android-studio scrollview

我有一个 Activity,它有一个包含 125 个按钮的 ScrollView。 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

最佳答案

尝试

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

关于java - 以编程方式滚动 ScrollView 以关注按钮或 View - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19778197/

相关文章:

java - 如何在Swing中控制JButton?

java - 如何打印给定数字 n 的魔术矩阵最多 n 方

android - 如何从设计支持 android 在选项卡布局中嵌入字体

Android studio 找不到 Theme.AppCompat.Light.DarkActionBar

用于基于 JSON 的 Web 服务的 Java 框架

java - 优先队列实现堆

java - Android:将普通菜单转换为 slider 菜单? (用最少的编码)

java - 启动 Intent 时出现 Android 运行时异常

android-studio - 为什么我的设计屏幕是灰色的,并且在 Android Studio 中只显示 "androidx.constraintlayout.ConstraintLayout"?

android - 如何在 Android Studio 中显示工具提示描述?