Set ScrollView + Fixed Button Programatically on A

2019-08-09 08:03发布

问题:

I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.

Actually i have this :

//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);

I do the first 3 page Google on "fixed button and scrollview Android programatically"

Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)

Thx for your help.

回答1:

try this

LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);

ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);

Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
        LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");

layout.addView(scroll);
layout.addView(btn);

setContentView(layout);


回答2:

If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.

http://developer.android.com/reference/android/widget/LinearLayout.html