android: relative layout two buttons occupy all the available horizontal space

前端 未结 6 1193
不知归路
不知归路 2021-02-13 11:06

i have a relative layout and i have two buttons in it with texts \"hello\" and \"world\" respectively. i want these two buttons to lie adjacent to each other and equally occupy

6条回答
  •  臣服心动
    2021-02-13 11:46

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;
    

    in relative layout you can achieve this dynamically.

    Button1.setWidth(width / 2);
    Button2.setWidth(width / 2);
    

提交回复
热议问题