Android Scroll View无法以编程方式运行?

时间:2013-10-02 08:06:11

标签: android android-layout android-scrollview

我尝试了以下代码,希望能够水平滚动。 总共创建了四个按钮,两个应该在屏幕上,另外两个应该滚动一次。 另外,我想在两边都用圆形包装。

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

1 个答案:

答案 0 :(得分:0)

需要修改3件才能使其正常工作(至少对我而言)

<强> 1。将ScollView更改为Horizo​​ntalScrollView:

HorizontalScrollView scrollView = new HorizontalScrollView(this);

<强> 2。更改Horizo​​ntalScrollView的LayoutParams:

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

(将宽度参数更改为WRAP_CONTENT,以便在必要时根据其子项的尺寸进行拉伸。)

第3。更改按钮的大小:

我不知道您的屏幕分辨率,至少该按钮不够宽,无法让scrollView在我的模拟器上工作。 (720 * 1280)

希望它有所帮助。

相关问题