문제

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

ImageView 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top