带文字和图像的按钮(Android)

时间:2011-07-22 16:45:35

标签: android image text button

我想创建一个包含图像和文本的android按钮,但文本会自动居中。如何将文本放在按钮的底部?

我知道我可以使用相对布局在图像下面放置第二个文本按钮,但我更喜欢最小化代码。

6 个答案:

答案 0 :(得分:43)

您可以声明要为图像指定按钮的位置 (我假设你已经在按钮上有图像):

<Button
   android:id="@+id/button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="- I'm a Button -"
   android:drawableTop="@drawable/icon"
   />

您还可以使用android:drawablePadding(int)

在文本和图像之间设置填充

drawableTop属性可以更改为drawableRight,Left,Bottom等。祝你好运!

答案 1 :(得分:4)

如果我是你,我就不会使用Button。使用LinearLayout或RelativeLayout,只需给它一个Button背景(提示:如果你希望它为每个状态设置不同的图像,请使用选择器)并将TextView放在其中,然后你可以使用drawableLeft,drawableTop等..属性把照片放在你想要的任何一方。如果您希望更高级别地控制图片相对于文本的位置,那么请使用一个TextView和一个ImageView。然后在你的java中,只需获得对你的布局的引用,就像使用setOnClickListener()按钮一样对待它。

答案 2 :(得分:4)

您也可以这样做

enter image description here

<LinearLayout
android:id="@+id/image_button_2"
style="@android:style/Widget.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:src="@drawable/ic_gear" />

<TextView
    android:id="@+id/image_button_2_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:text="Button 2" />

    </LinearLayout>

在您的活动中

 final View button2 = findViewById(R.id.image_button_2);
 button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

答案 3 :(得分:1)

enter image description here

<Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:background="@drawable/home_button"
        android:drawableLeft="@android:drawable/ic_menu_edit"
        android:drawablePadding="6dp"
        android:gravity="left|center"
        android:height="60dp"
        android:padding="6dp"
        android:text="AndroidDhina"
        android:textColor="#000"
        android:textStyle="bold" />

了解更多信息http://androiddhina.blogspot.in/2015/09/how-to-add-image-inside-button.html

答案 4 :(得分:0)

您可以尝试这样的事情

<Button
    android:id="@+id/ButtonTest"
    android:text="this is text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/icon"
    **android:gravity="bottom"**/>

答案 5 :(得分:0)

我认为实现这一目标的最简单方法(使用纯XML)是:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickMethod" 
        android:orientation="vertical" 
        android:clickable="true">
   <ImageView
        android:src="@drawable/YOUR_DRAWABLE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:clickable="false"/>
   <TextView
         android:text="@string/YOUR_TEXT"  
         android:layout_width="match_parent"
         android:layout_height="wrap_content"           
         android:clickable="false"/>
</LinearLayout>
相关问题