0

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

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

1 回答 1

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)

希望能帮助到你。

于 2013-10-02T09:47:48.520 回答