How to have Image and Text Center within a Button

后端 未结 12 2180
感动是毒
感动是毒 2020-12-02 15:48

I want to display TEXT and Icon on a Button.

+----------------------------+
|          Icon TEXT         |
+---------------------         


        
相关标签:
12条回答
  • 2020-12-02 15:53

    This is a hack, but worked for me with a negative margin:

    <Button
        android:id="@+id/some_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/some_drawable"
        android:drawablePadding="-118dp"
        android:paddingEnd="28dp"
        android:text="@string/some_string" />
    

    A better way would probably be doing this in a custom view

    0 讨论(0)
  • 2020-12-02 15:54

    You can just set a padding depending on button size and image size:

    Button button1 = null;
    //initialize button….
    ViewGroup.LayoutParams params = button1.getLayoutParams();
    int btn1Width = ((int) (0.33 * (double)ecranWidth));
    params.width = btn1Width;
    button1.setLayoutParams(params);
    button1.setPadding((btn1Width/2-9), 0, 0, 0);
    //where (btn1Width/2-9)   =   size of button divided on 2 minux half size of icon… 
    
    0 讨论(0)
  • 2020-12-02 15:56

    The easy way (albeit not perfect) is to set the paddingRight to the same width as your icon.

    0 讨论(0)
  • 2020-12-02 15:57
    android:layout_gravity="center_vertical|center_horizontal|center" >
    

    0 讨论(0)
  • 2020-12-02 16:01

    This should work

    <LinearLayout        
    
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal">    
    <TextView          
        android:id="@+id/button_text"        
        android:layout_width="wrap_content"         
        android:layout_height="wrap_content"        
        android:layout_centerInParent="true"        
         android:text="hello" />    
     <ImageView        
         android:layout_width="wrap_content"        
         android:layout_height="wrap_content"
         android:paddingBottom="10dip"
    />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 16:02

    You can fake it by making a more complex layout, but I'm not sure whether it's worth it. Here's something I hacked together:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignTop="@+id/foreground"
        android:layout_alignBottom="@id/foreground"
        android:layout_alignRight="@id/foreground"
        android:layout_alignLeft="@id/foreground"
        android:onClick="clickedMe" />
       <RelativeLayout
            android:id="@id/foreground"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <TextView  
            android:id="@+id/button_text"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" 
            android:text="@string/hello" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/button_text"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:src="@drawable/icon" />
    </RelativeLayout>
    </RelativeLayout>
    

    There might be a more concise way to do it. I tend to struggle getting RelativeLayout to do what I want sometimes. Note that you need to pay attention to the z-order (Button needs to appear first in the top level RelativeLayout) and you might need to adjust padding to get it to look the way you want.

    0 讨论(0)
提交回复
热议问题