LinearLayout image alignment by code

The question is simple, I dynamically create an image using code.

ImageView btnSend = new ImageView (this);

and add it to LinearLayout, the problem is that I want to leave the right alignment

how to do it?

Thanks in advance

+3
source share
2 answers

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 it params.gravitysets the gravity of the content or layout_gravity. Layout_gravity is what you want to change in this instance.

+4
source

You need to call setGravity () for LinearLayout:

yourLinLay.setGravity(Gravity.RIGHT);

or in XML format:

<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

+1

Source: https://habr.com/ru/post/1783883/


All Articles