简体   繁体   中英

Relative Layout width/height is not work programmatically?

I want to doing a relative layout programmatically and set android:layout_widht=60 android:layout_height=60.When i doing programmatically it is fill all screen? How can i do that?

my code:`

      RelativeLayout relativeLayout=new RelativeLayout(getContext());
        RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rel_btn.height = 60;
        rel_btn.width = 60;
        relativeLayout.setLayoutParams(rel_btn);
 this.setBackgroundResource(com.example.R.drawable.line);`

screen: 在此输入图像描述

btn.getLayoutParams().width = width;
btn.getLayoutParams().height = height;

in fact, just use

RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
            60, 60);

that's OK. the problem maybe the background pic stretch the button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM