简体   繁体   English

Android:图像按钮图标和文字

[英]Android: Image Button icon and text

I'm having trouble with the layout of my navigation bar. 我在导航栏的布局上遇到麻烦。 I was hoping to have an icon with text underneath it all centered in the button. 我希望有一个图标,其下方的所有文字均位于按钮的中心。 Currently when the icon is drawn in it pushes the text off the page. 当前,当在其中绘制图标时,会将文本推离页面。 The only way to get it back is by using android:drawablePadding="" (from here ) using a negative value but it looks like this: 取回它的唯一方法是使用负值使用android:drawablePadding =“”(从此处开始 ),但看起来像这样:

在此处输入图片说明

My xml looks like this: 我的xml看起来像这样:

<LinearLayout android:id="@+id/navbar"
    android:layout_alignParentBottom="true" android:gravity="center"
    android:layout_width="fill_parent" android:background="#001"
    android:layout_height="60dip" android:layout_weight="1"
    android:orientation="horizontal">

    <Button android:layout_height="fill_parent" android:id="@+id/navhomebtn" android:enabled="false" 
        android:textColor="#FFF" android:background="@drawable/navbuttonselected"   android:drawableTop="@drawable/homeicon" 
         android:drawablePadding="-40dip"
        android:text="Home" android:layout_weight=".25" android:layout_width="0dip" />

    <Button android:layout_height="fill_parent" android:id="@+id/navsearchbtn"
        android:textColor="#FFF" android:background="@drawable/navbuttons" 
        android:text="Search" android:layout_weight=".25"
        android:layout_width="0dip" />

    <Button android:layout_height="fill_parent" android:id="@+id/navfavbtn"
        android:textColor="#FFF" android:background="@drawable/navbuttons"
        android:text="Favorites" android:layout_weight=".25"
        android:layout_width="0dip" />
    <Button android:layout_height="fill_parent" android:id="@+id/navlistbtn"
        android:textColor="#FFF" android:background="@drawable/navbuttons"
        android:text="Loans" android:layout_weight=".25"
        android:layout_width="0dip" />

</LinearLayout>

The way your buttons look you might as well use a relativelayout containing a textview and an imageview. 您的按钮外观可能还需要使用包含textview和imageview的相对布局。 Then set the relativelayouts clickable attribute to true and the background attribute to your navbuttons drawable. 然后将relativelayouts clickable属性设置为true,将background属性设置为navbuttons drawable。

Another way is to use the drawableLeft/Right/Top/Bottom attributes like this: 另一种方法是使用drawableLeft / Right / Top / Bottom属性,如下所示:

<Button 
    android:id="@+id/home_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/home_icon"
    android:background="@drawable/nav_background"
    android:text="Home"/> 

To set text and image for button you may do like this: 要为按钮设置文本和图像,您可以这样做:

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:text="Button"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon"></Button>
    </LinearLayout>

Hope, it helps you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM