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