Set width of Button in Android

后端 未结 5 1090
没有蜡笔的小新
没有蜡笔的小新 2021-02-12 22:04

How can I set a fixed width for an Android button? Everytime I try to set a fixed width it fills the current parent (the RelativeView). Here is my XML:



        
5条回答
  •  长发绾君心
    2021-02-12 23:00

    Instead of android:layout_width="wrap_content" you can use a fixed pixelsize like for example android:layout_width="20px" or a better way: android:layout_width="20dp"

    you can also set the width programatically in the code: Button.setWidth(int width)

    Concerning your update:

    I don't know if it is a good solution, but i works. You could use

     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);
    

    to read the screen resolution and then set the button sizes according to metrics.widthPixel and metrics.heightPixel

提交回复
热议问题