How to have dynamically a fixed header with scrollable content in Android?

孤街醉人 提交于 2019-12-10 23:54:34

问题


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
}

Thanks!


回答1:


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)

EDIT:

your new 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
    }

EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment



来源:https://stackoverflow.com/questions/33754968/how-to-have-dynamically-a-fixed-header-with-scrollable-content-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!