简体   繁体   中英

Android Button text gets cut off: solutions?

I have a problem... I want to display a list of buttons that represent letters. Something like:

[a] [b] [c] [d] [e] ....

The problem is that the buttons are really small and the text gets cut off because of the padding of the button object. Is there a way to always display the button's text sort of on top of the button, so it doesn't get cut off?

My only alternative seems to create JPEGs which mimic the buttons. But that is not elegant at all.

Another option would be to show a TextView on top of the button, which may by a little more elegant that the previous choice.

for (...) {
   Button buton = new Button(Activity.this);
   LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
   int margin = (int) Math.round(5 * SCALE);
   layoutParams.setMargins(margin, margin, margin, margin);
   buton.setLayoutParams(layoutParams);
   buton.setOnClickListener(new View.OnClickListener() { ... }
   (...)
   linearLayout.add(buton)
}

What should I do?

EDIT: I create all my Buttons inside a LinearLayout (horizontal) programatically. I don't know the number of buttons, so that's why I'm setting a weight. And I'm targeting API at least 8 (2.2).

You can add padding manually..you can also set width and height of the button, so that text don't disappear.. something like this

    <Button
        android:id="@+id/button1"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:text="This is a short text" />

or you can change margin like this

<Button
    android:layout_marginLeft="-3dip"
    android:layout_marginRight="-3dip"
    ... />

please check & modify all properties below, i think you should change elevation , insetTop , insetBottom :

<item name="elevation">0dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:paddingTop">6dp</item>
<item name="android:paddingBottom">6dp</item>
<item name="android:minHeight">32dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>

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