Set ScrollView + Fixed Button Programatically on Android with setText.

删除回忆录丶 提交于 2019-12-11 04:35:18

问题


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



来源:https://stackoverflow.com/questions/8268059/set-scrollview-fixed-button-programatically-on-android-with-settext

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