2011-01-06 86 views
3

問題很簡單,我使用代碼動態創建一個imageview。在LinearLayout中調整Imageview的代碼

ImageView btnSend = new ImageView (this); 

,並把它添加到LinearLayout中,問題是,我要離開右對齊

如何做到這一點?

在此先感謝

回答

4

嘗試使用L ayoutParams:

LinearLayout rootLayout = (LinearLayout)findViewById(R.id.root_layout); 

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
params.gravity = Gravity.RIGHT; 

ImageView btnSend = new ImageView (this); 
btnSend.setLayoutParams(params); 
rootLayout.addView(btnSend); 

唯一的問題是,我不記得是否params.gravity設置內容重力或layout_gravity。 Layout_gravity是你想在這個實例中改變的。

+0

謝謝,作品完美! – seba123neo 2011-01-07 14:10:20