简体   繁体   中英

Changing the text of an android button also changes its size

I have two android buttons for which I fixed their size in XML with the following code:

<Button
    android:id="@+id/start_button"
    android:layout_width="120dip"
    android:layout_height="wrap_content"
    android:layout_below="@+id/player_name"
    android:layout_marginTop="29dp"
    android:layout_toLeftOf="@+id/player_name"
    android:onClick="startResetButtonOnClick"
    android:text="@string/start_button_label" />

Everything is good when I first create the activity but as soon as I change the text of the button it gets resized. What am I doing wrong?

Try to add android:textSize="25sp" to that. At least the text remains at the same size. Hope it works

You are setting the size of the button's height to wrap_content .

This makes the buttons size fit it's text.

Change wrap_content to a certain amount of pixels to get the button to stay a certain size.

I discovered that my issue was caused by the layout I used. I am using a RelativeLayout and I was placing buttons with respect to the borders of a text field that contained changing text and thus was resizing itself.

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