简体   繁体   中英

Programmatically set gravity to vertically center content of HorizontalScrollView

I have a HorizontalScrollView containing a LinearLayout. I can't seem to get the content to vertically center inside my scrollview, I can't set gravity on the horizontalscrollview, even though I tried with a LinearLayout.LayoutParams when setting the scrollview as contentview.

Can anyone help?

This is what I have:

HorizontalScrollView sv = new HorizontalScrollView(c);

LinearLayout llh = new LinearLayout(c); llh.setOrientation(LinearLayout.HORIZONTAL);

sv.addView(llh, llh_lp)

llh_lp is just simple wrap_content params.

setContentView(sv)

And I tried adding linearlayout.layoutparams with gravity = gravity.center_vertical too, on the setContentView call.

Try this way..

HorizontalScrollView sv = new HorizontalScrollView(this);
    sv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    LinearLayout llh = new LinearLayout(this); 
    llh.setOrientation(LinearLayout.HORIZONTAL);
    llh.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    llh.setGravity(Gravity.CENTER_VERTICAL);
    TextView tv = new TextView(this);
    tv.setText("Policia Centeras");
    tv.setBackgroundColor(-16776961);

    llh.addView(tv);
    sv.addView(llh);
    setContentView(sv);

I hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM