Why does maxWidth on a Button not work and how to work around it?

坚强是说给别人听的谎言 提交于 2019-12-05 00:20:29
Ronnie

Remove the android:layout_weight="1" line from both buttons.
Add android:minWidth="50dp" and android:layout_width="wrap_content"

EDIT:

You need to calculate the button sizes manually depending on the screen size. First you need to have a standard screen size set. For example, If the you develop the app on a screen width 400px, and the button is 100px wide, then the formula for maintaining the same button width to screen width ratio on all devices will be as follows

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 buttonWidth = 100 * (metrics.widthPixels/400); 

Use case:
If screen width = 480px, then button width should be 120px.

The good way of solving this is to create multiple layout for different combination of screen resolution. When you get at some max stretch, create a new layout that have fixed value where needed. See the section about "Use layout alias" in http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters

Try setting the layout_width to wrap_content vs. 0dp.

Width/Height seem to always ahve to be set together,.. This is working in my view

        <Button
            android:text="Center"
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:id="@+id/selectionCenterButton"
            android:minWidth="50dp"
            android:minHeight="50dp"
            android:maxWidth="100dp"
            android:maxHeight="50dp"
            android:layout_weight="1" />

The button's parent is set to wrap content, so scales down, but up to a max of 400 wide (4 buttons)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!