简体   繁体   English

以编程方式将重力设置为Horizo​​ntalScrollView的垂直居中内容

[英]Programmatically set gravity to vertically center content of HorizontalScrollView

I have a HorizontalScrollView containing a LinearLayout. 我有一个包含LinearLayout的Horizo​​ntalScrollView。 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. 我似乎无法使内容在滚动视图内垂直居中,即使在将scrollview设置为contentview时尝试使用LinearLayout.LayoutParams,也无法在horizo​​ntalscrollview上设置重力。

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. llh_lp只是简单的wrap_content参数。

setContentView(sv)

And I tried adding linearlayout.layoutparams with gravity = gravity.center_vertical too, on the setContentView call. 我也尝试在setContentView调用中添加带有gravity = gravity.center_vertical的linearlayout.layoutparams。

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. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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