开发者

Align Imageview in LinearLayout by code

开发者 https://www.devze.com 2023-02-03 01:41 出处:网络
the question is simple, i am creating a imageview dynamically using code. ImageV开发者_开发技巧iew btnSend = new ImageView (this);

the question is simple, i am creating a imageview dynamically using code.

ImageV开发者_开发技巧iew btnSend = new ImageView (this);

and add it to a LinearLayout, the problem is that I want to leave right-aligned

how to do that?

Thanks in advance


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

The only problem is that I don't remember if params.gravity sets the content gravity or layout_gravity. Layout_gravity is what you're wanting to change in this instance.


You have to call setGravity() on LinearLayout:

yourLinLay.setGravity(Gravity.RIGHT);

or in 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

0

精彩评论

暂无评论...
验证码 换一张
取 消