首页 文章

Android:在Button或ImageButton上组合文本和图像

提问于
浏览
502

我正在尝试在按钮上创建一个图像(作为背景)并动态添加,具体取决于运行时发生的情况,图像上方/上方的一些文本 .

如果我使用 ImageButton 我甚至没有可能添加文字 . 如果我使用 Button 我可以添加文本,但只定义一个带有 android:drawableBottom 的图像和类似here的类似XML属性 .

然而,这些属性仅在x和y维度中组合文本和图像,这意味着我可以在文本周围绘制图像,但不能在我的文本下方/下方(z轴定义为从显示中出来) .

有关如何做到这一点的任何建议?一个想法是扩展 ButtonImageButton 并覆盖 draw() -method . 但凭借我目前的知识水平,我真的不知道如何做到这一点(2D渲染) . 也许有经验的人知道解决方案或至少有一些指针可以开始?

14 回答

  • 5

    您可以使用 drawableTop (也是 drawableLeft 等)为图像添加 gravity left|center_vertical 在图像下方设置文本

    <Button
                android:id="@+id/btn_video"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@null"
                android:drawableTop="@drawable/videos"
                android:gravity="left|center_vertical"
                android:onClick="onClickFragment"
                android:text="Videos"
                android:textColor="@color/white" />
    
  • 5

    只需使用LinearLayout并假装它是 Button - 设置 background 并且可点击是关键:

    <LinearLayout
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_default"
        android:clickable="true"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:src="@drawable/image" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"
            android:text="Do stuff" />
    </LinearLayout>
    
  • 196
    <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" />
    
  • 20

    可能我的解决方案适合很多用户,我希望如此 .

    我建议你用你的风格制作TextView . 它非常适合我,并且具有所有功能,如按钮 .

    首先让make按钮样式,你可以在任何地方使用...我正在创建button_with_hover.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
            <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="#8dbab3" />
                <gradient android:angle="-90" android:startColor="#48608F" android:endColor="#48608F"  />
            </shape>
    
            <!--#284682;-->
            <!--border-color: #223b6f;-->
        </item>
        <item android:state_focused="true">
            <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="#284682" />
                <solid android:color="#284682"/>
            </shape>
        </item>
        <item >
            <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="@color/ControlColors" />
                <gradient android:angle="-90" android:startColor="@color/ControlColors" android:endColor="@color/ControlColors" />
            </shape>
        </item>
    
    </selector>
    

    其次,让我们创建一个textview按钮 .

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_gravity="right|bottom"
        android:gravity="center"
        android:padding="12dip"
        android:background="@drawable/button_with_hover"
        android:clickable="true"
        android:drawableLeft="@android:drawable/btn_star_big_off"
        android:textColor="#ffffffff"
        android:text="Golden Gate" />
    

    这就是结果 . 然后使用任何颜色或任何其他属性和边距设置自定义按钮的样式 . 祝好运

  • -3
    <Button android:id="@+id/imeageTextBtn" 
            android:layout_width="240dip"
            android:layout_height="wrap_content"
            android:text="Side Icon With Text Button"
            android:textSize="20sp"
            android:drawableLeft="@drawable/left_side_icon"         
            />
    
  • 59

    只是替换

    android:background="@drawable/icon"
    

    android:background="@android:color/transparent"
    android:drawableTop="@drawable/[your background image here]"
    

    izz是一个很好的技巧..;)

  • 571

    这个问题有一个更好的解决方案 .

    只需采用正常的 Button 并使用 drawableLeftgravity 属性 .

    <Button
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:drawableLeft="@drawable/my_btn_icon"
      android:gravity="left|center_vertical" />
    

    通过这种方式,您可以获得一个按钮,该按钮在按钮的左侧显示一个图标,并且图标的右侧站点的文本垂直居中 .

  • 323

    我从这里提到的方法采取了不同的方法,它工作得非常好,所以我想分享它 .

    我正在使用Style创建一个自定义按钮,左侧是图像,右侧是文本 . 只需按照下面的4个“简单步骤”:

    I.使用至少3个不同的PNG文件和您拥有的工具创建9个补丁:/YOUR_OWN_PATH/android-sdk-mac_x86/tools/./draw9patch . 在此之后你应该:

    button_normal.9.png,button_focused.9.png和button_pressed.9.png

    然后下载或创建一个24x24 PNG图标 .

    ic_your_icon.png

    将所有内容保存在Android项目的drawable /文件夹中 .

    II . 在drawable /文件夹下的项目中创建名为button_selector.xml的XML文件 . 各州应该是这样的:

    <item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/button_focused" />
    <item android:drawable="@drawable/button_normal" />
    

    III . 转到values /文件夹并打开或创建styles.xml文件并创建以下XML代码:

    <style name="ButtonNormalText" parent="@android:style/Widget.Button">
        <item name="android:textColor" >@color/black</item>
        <item name="android:textSize" >12dip</item>
        <item name="android:textStyle" >bold</item>
        <item name="android:height" >44dip</item>
        <item name="android:background" >@drawable/button_selector</item>
        <item name="android:focusable" >true</item>
        <item name="android:clickable" >true</item>
    </style>
    
    <style name="ButtonNormalTextWithIcon" parent="ButtonNormalText">
        <item name="android:drawableLeft" >@drawable/ic_your_icon</item>
    </style>
    

    ButtonNormalTextWithIcon是一个“子样式”,因为它正在扩展ButtonNormalText(“父样式”) .

    请注意,将ButtonNormalTextWithIcon样式中的drawableLeft更改为drawableRight,drawableTop或drawableBottom,可以将图标放置在相对于文本的其他位置 .

    IV . 转到您拥有用于UI的XML的布局/文件夹,然后转到要应用样式的Button,并使其如下所示:

    <Button android:id="@+id/buttonSubmit" 
    android:text="@string/button_submit" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="@style/ButtonNormalTextWithIcon" ></Button>
    

    而且......瞧!你的按钮左侧有一个图像 .

    对我来说,这是更好的方法!因为这样做,您可以与要显示的图标分开管理按钮的文本大小,并使用相同的背景可绘制几个按钮,使用不同的图标,使用样式的Android UI准则 .

    您还可以为应用程序创建主题并为其添加“父样式”,以便所有按钮看起来都相同,并将“子样式”应用于仅在您需要的位置 .

  • 49

    你可以用这个:

    <Button
                        android:id="@+id/reset_all"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_weight="1"
                        android:background="@drawable/btn_med"
                        android:text="Reset all"
                        android:textColor="#ffffff" />
    
                    <Button
                        android:id="@+id/undo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:background="@drawable/btn_med"
                        android:text="Undo"
                        android:textColor="#ffffff" />
    

    因为我把图像作为 background 并添加了文字..!

  • 10

    您可以在 Button 上调用 setBackground() 来设置按钮的背景 .

    任何文本都将显示在背景上方 .

    如果你在xml中寻找类似的东西,那么: android:background 属性的工作方式相同 .

  • 0
    <Button
         android:id="@+id/groups_button_bg"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="Groups"
         android:drawableTop="@drawable/[image]" />
    
    
    android:drawableLeft
    android:drawableRight
    android:drawableBottom
    android:drawableTop
    

    http://www.mokasocial.com/2010/04/create-a-button-with-an-image-and-text-android/

  • 6

    这段代码非常适合我

    <LinearLayout
        android:id="@+id/choosePhotosView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:clickable="true"
        android:background="@drawable/transparent_button_bg_rev_selector">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/choose_photo"/>
    
         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="@string/choose_photos_tv"/>
    
    </LinearLayout>
    
  • 41
    <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/temp"
            />
    
  • 12

    For users who just want to put Background, Icon-Image and Text in one Button 来自不同文件:设置 Button 背景,drawableTop / Bottom / Rigth / Left和填充属性 .

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_test"
            android:drawableTop="@drawable/home_icon_test"
            android:textColor="#FFFFFF"
            android:id="@+id/ButtonTest"
            android:paddingTop="32sp"
            android:drawablePadding="-15sp"
            android:text="this is text"></Button>
    

    对于更复杂的排列,您也可以使用 RelativeLayout 并使其可点击 .

    Tutorial: 涵盖两种情况的精彩教程:http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx

相关问题