2011-11-03 43 views
0

我有一個包含LinearLayout的Horizo​​ntalScrollView。我似乎無法讓內容垂直居中在我的滾動視圖中,我無法在horizo​​ntalscrollview上設置重力,即使我在使用LinearLayout.LayoutParams時將scrollview設置爲contentview。以編程方式將重力設置爲Horizo​​ntalScrollView的垂直居中內容

任何人都可以幫忙嗎?

這是我有:

HorizontalScrollView sv = new HorizontalScrollView(c);

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

sv.addView(llh, llh_lp)

llh_lp只是簡單WRAP_CONTENT PARAMS。

setContentView(sv)

我試着用重力加linearlayout.layoutparams = gravity.center_vertical過,在調用的setContentView。

回答

6

嘗試這種方式..

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); 

我希望它可以幫助你。

相關問題