簡體   English   中英

通過代碼在LinearLayout中對齊Imageview

[英]Align Imageview in LinearLayout by code

問題很簡單,我正在使用代碼動態創建一個imageview。

ImageView btnSend = new ImageView (this);

並將其添加到LinearLayout,問題是我想要保持右對齊

怎么做?

提前致謝

嘗試使用LayoutParams:

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是您希望在此實例中更改的內容。

你必須在LinearLayout上調用setGravity():

yourLinLay.setGravity(Gravity.RIGHT);

或者用XML:

<LinearLayout android:gravity="right">

http://developer.android.com/reference/android/widget/LinearLayout.html#setGravity%28int%29 http://developer.android.com/reference/android/view/Gravity.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM