如何在Button或ImageButton下方放置文本? [英] How to place text below a Button or a ImageButton?

查看:696
本文介绍了如何在Button或ImageButton下方放置文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Buttons(或ImageButtons)来显示我的应用程序的图标.我使用它来设置按钮的状态,其中一个图像用于按下按钮,另一图像用于未按下按钮:

i'm using Buttons (or ImageButtons) to show the icons of my app. I use this to set the state of the buttons, with a image for the button pressed and another image for the button unpressed:

selector(this, favorites,  R.drawable.icon_star_mark,  R.drawable.icon_star_mark_selected);

public static void selector(Context c, Button b, int normal_image, int pressed_image){
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[] {android.R.attr.state_pressed}, c.getResources().getDrawable(pressed_image));         
        states.addState(new int[] { }, c.getResources().getDrawable(normal_image));      
        b.setBackgroundDrawable(states);
    }

我需要在图标下放置文本,但是我想学习如何在不使用带有图标和下面的textview的LinearLayout的情况下进行操作.如果可能的话,我想学习这样做.

I need to put text below the icon, but i want to learn doing it without using a LinearLayout with a icon and a textview below it. I want to learn doing that if it is possible to achiev it.

这是我创建图标的方式:

This is the way i'm creating the icon:

Button map= new Button(this);
selector(this, map,  R.drawable.icon_map,  R.drawable.icon_map_selected);
map.setText("Map");

问题在于文本显示在图标上方,而不是图标下方.

The problem is that the text is being displayed above the icon, and not below the icon.

有可能用Java代码来实现吗? (将图标文字放置在图标下方,而无需在图标下方使用带有文本视图的线性布局)

It is possible to achieve that with java code? (placing the text of the icon below the icon without using a linear layout with a textview below the icon)

预先感谢

推荐答案

可以使用drawableTop属性.选择器进入android:background,图标进入android:drawableTop attr.

It's possible using drawableTop attribute. The selector goes into android:background and the icon into android:drawableTop attr.

<Button
        android:id="@+id/btn"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/any_drawable"
        android:text="@string/any_text" />

或者您可以使用 button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

Or you can set drawables programatically using button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

这篇关于如何在Button或ImageButton下方放置文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆