问题描述
我已经程序创建一个 RelativeLayout的
,其中包含一个按钮。我还创建了一个滚动型
包含一个的LinearLayout
,其中超过10 TextViews
。我想有 RelativeLayout的
对齐顶部和固定。当有人试图向下滚动,我希望所有的 TextViews
去后面的固定 RelativeLayout的
。我想这个按钮是始终可见。有了这个code时, RelativeLayout的
和按钮不会显示。我在哪里错了?_爱
RelativeLayout的(固定的)结果
- 巴顿结果
的LinearLayout结果
- 滚动型结果
- TextView的结果
- +其他10个TextViews结果
这里是我的code:
I have created programmatically a RelativeLayout
which contains a button. I have also created a ScrollView
which contains a LinearLayout
in which are more than 10 TextViews
. I want to have the RelativeLayout
to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews
to go behind the fixed RelativeLayout
. I want that button to be always visible. With this code, the RelativeLayout
and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
@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
}
谢谢!
推荐答案
在您的code更换了与滚动型的RelativeLayout的。刚刚成立第一部分的LinearLayout作为内容查看,然后通过 addView(RelativeLayout的)
及认沽有你滚动视图也可以通过 addView(滚动视图)
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout)
and the put there you scrollView also via addView(scrollView)
编辑:
新的code:
@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
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment
这篇关于如何动态地与Android中滚动内容固定头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!