开发者

How to set android:layout_alignParentLeft attribute to imageview in code?

开发者 https://www.devze.com 2023-02-07 06:30 出处:网络
I want to achieve the same thing in code as it is in this xml (this imageview 开发者_开发知识库is inside relative layout):

I want to achieve the same thing in code as it is in this xml (this imageview 开发者_开发知识库is inside relative layout):

                          <ImageView   
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/logo"/>

I know how to set layout width and height, but don't know how to set layout_alignParentLeft="true".

Is it possbile to do this in code?


RelativeLayout.LayoutParams params = 
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
imageView.setLayoutParams(params);


When adding an element to a layout in code, you can provide LayoutParams. In this case, you want to use RelativeLayout.LayoutParms.

You can add a rule to the params to tell it that youwant to ALIGN_PARENT_LEFT.

0

精彩评论

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