简体   繁体   English

Android Scroll View无法以编程方式工作?

[英]Android Scroll View not working programmatically?

I tried the following code hoping to scroll horizontally. 我尝试了以下代码,希望可以水平滚动。 A total of four buttons are created, Two should be on screen and the other two should come once scrolled. 总共创建了四个按钮,其中两个应该出现在屏幕上,另外两个应该滚动一次。 Additionally, i want circular wrapping in both side. 另外,我要在两侧进行圆形包装。

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        LinearLayout rootlayout = new LinearLayout(this);
        rootlayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        rootlayout.setBackgroundColor(Color.parseColor("#5b331d"));
        rootlayout.setOrientation(LinearLayout.VERTICAL);
        rootlayout.setPadding(pxToDp(50), 0, pxToDp(50), 0);
        setContentView(rootlayout);

        LinearLayout historyLayout = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, pxToDp(100));
//      params.weight=0.2f;
        historyLayout.setLayoutParams(params);
        historyLayout.setOrientation(LinearLayout.HORIZONTAL);
        historyLayout.setBackgroundColor(Color.parseColor(("#fbe0d9")));
        rootlayout.addView(historyLayout);

        ScrollView scrollView = new ScrollView(this);
        LinearLayout.LayoutParams scrollParams = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))*2,LayoutParams.MATCH_PARENT);
//      scrollParams.weight = 0.5f;
        scrollView.setLayoutParams(scrollParams);
        scrollView.setBackgroundColor(Color.GREEN);
        scrollView.setFillViewport(true);
//      scrollView.setSmoothScrollingEnabled(true);
//      scrollView.canScrollHorizontally(1);
        rootlayout.addView(scrollView);


        LinearLayout historyLayout1 = new LinearLayout(this);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//      params1.weight=0.5f;
        historyLayout1.setLayoutParams(params1);
        historyLayout1.setOrientation(LinearLayout.HORIZONTAL);
        historyLayout1.setBackgroundColor(Color.parseColor(("#bfcada")));
        scrollView.addView(historyLayout1);
//      rootlayout.addView(historyLayout1);


        Button button1 = new Button(this);
        LinearLayout.LayoutParams buttomParam1 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button1.setLayoutParams(buttomParam1);
        button1.setText("OK");
        button1.setBackgroundColor(Color.GRAY);
        historyLayout1.addView(button1);

        Button button2 = new Button(this);
        //LinearLayout.LayoutParams buttomParam2 = new LinearLayout.LayoutParams(pxToDp(250), LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams buttomParam2 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button2.setLayoutParams(buttomParam2);
        button2.setText("OK");
        button2.setBackgroundColor(Color.RED);
        historyLayout1.addView(button2);

        Button button3 = new Button(this);
        LinearLayout.LayoutParams buttomParam3 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button3.setLayoutParams(buttomParam3);
        button3.setText("OK");
        button3.setBackgroundColor(Color.GRAY);
        historyLayout1.addView(button3);

        Button button4 = new Button(this);
        LinearLayout.LayoutParams buttomParam4 = new LinearLayout.LayoutParams((getWidthDp()-pxToDp(100))/2, LayoutParams.MATCH_PARENT);
        button4.setLayoutParams(buttomParam4);
        button4.setText("OK");
        button4.setBackgroundColor(Color.RED);
        historyLayout1.addView(button4);


        //setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to
        // the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private static int dpToPx(int dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    private static int pxToDp(int px)
    {
        return (int) (px / Resources.getSystem().getDisplayMetrics().density);
    }

    private int getWidthDp()
    {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        return pxToDp(size.x);
    }
    private int getHeightDp()
    {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        return pxToDp(size.y);
    }
}

3 things to modify to make it work, (at least for me it is) 进行3件事修改以使其正常运行(至少对我来说是这样)

1. Change ScollView to HorizontalScrollView : 1.将ScollView更改为Horizo​​ntalScrollView:

HorizontalScrollView scrollView = new HorizontalScrollView(this);

2. Change the LayoutParams of your HorizontalScrollView : 2.更改您的Horizo​​ntalScrollView的LayoutParams:

LinearLayout.LayoutParams scrollParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);

(Change the width param to WRAP_CONTENT so it would stretch when necessary according to the dimension of it's child.) (将width参数更改为WRAP_CONTENT,以便在必要时根据其子级的尺寸进行拉伸。)

3. Change the size of your button : 3.更改按钮的大小:

I don't know your screen resolution, at least the button won't be wide enough for scrollView to work on my emulator. 我不知道您的屏幕分辨率,至少按钮的宽度不足以使scrollView在我的模拟器上正常工作。 (720*1280) (720 * 1280)

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM