我以编程方式创建了一个包含按钮的RelativeLayout
。我还创建了一个ScrollView
,其中包含LinearLayout
,其中TextViews
超过10 RelativeLayout
。我希望将TextViews
对齐并固定。当有人试图向下滚动时,我希望所有RelativeLayout
都在固定RelativeLayout
之后。我希望该按钮始终可见。使用此代码,不会显示@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
和按钮。我哪里错了?
RelativeLayout(固定)
- 按钮
的LinearLayout
- ScrollView
- TextView
- +其他10个TextViews
这是我的代码:
border
谢谢!
答案 0 :(得分:0)
在您的代码中,用ScrollView替换RelativeLayout。首先将一些LinearLayout设置为contentView,然后通过addView(relativeLayout)
将RelativeLayout放在那里,然后通过addView(scrollView)
放置滚动视图
编辑:
您的新代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
编辑2:根据评论将第一个linearLayout重命名为mainLinearLayout