Android Layout: width half of parent

前端 未结 5 521
野的像风
野的像风 2021-02-02 06:36

I have a very simple layout that I can\'t make it looks like I want. It\'s a LinearLayout with a button and a Switch. I want them to show one above the other, but I want their w

5条回答
  •  忘了有多久
    2021-02-02 06:56

    Button button = (Button) findViewById(R.id.share_button);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int buttonWidth = width/2;
    

    set this buttonWidth to your button like button.setWidth(buttonWidth);

提交回复
热议问题