[英]Programmatically set gravity to vertically center content of HorizontalScrollView
我有一個包含LinearLayout的HorizontalScrollView。 我似乎無法使內容在滾動視圖內垂直居中,即使在將scrollview設置為contentview時嘗試使用LinearLayout.LayoutParams,也無法在horizontalscrollview上設置重力。
有人可以幫忙嗎?
這就是我所擁有的:
HorizontalScrollView sv = new HorizontalScrollView(c);
LinearLayout llh = new LinearLayout(c);
llh.setOrientation(LinearLayout.HORIZONTAL);
sv.addView(llh, llh_lp)
llh_lp只是簡單的wrap_content參數。
setContentView(sv)
我也嘗試在setContentView調用中添加帶有gravity = gravity.center_vertical的linearlayout.layoutparams。
嘗試這種方式..
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);
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.