How to show the text on a ImageButton?

后端 未结 11 1418
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 08:42

I have an ImageButton and I want to show a text and an image on it. But when I try on emulator:



        
相关标签:
11条回答
  • 2020-12-04 09:14

    ImageButton can't have text (or, at least, android:text isn't listed in its attributes).

    The Trick is:

    It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

    0 讨论(0)
  • 2020-12-04 09:17

    Actually, android:text is not an argument accepted by ImageButton but, If you're trying to get a button with a specified background (not android default) use the android:background xml attribute, or declare it from the class with .setBackground();

    0 讨论(0)
  • 2020-12-04 09:20

    As you can't use android:text I recommend you to use a normal button and use one of the compound drawables. For instance:

    <Button 
        android:id="@+id/buttonok" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/buttonok"
        android:text="OK"/>
    

    You can put the drawable wherever you want by using: drawableTop, drawableBottom, drawableLeft or drawableRight.

    UPDATE

    For a button this too works pretty fine. Putting android:background is fine!

    <Button
        android:id="@+id/fragment_left_menu_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg"
        android:text="@string/login_string" />
    

    I just had this issue and is working perfectly.

    0 讨论(0)
  • 2020-12-04 09:20

    I solved this by putting the ImageButton and TextView inside a LinearLayout with vertical orientation. Works great!

    <LinearLayout
        android:id="@+id/linLayout"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <ImageButton
            android:id="@+id/camera_ibtn"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/camera" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/take_pic"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 09:21

    Best way:

    <Button 
    android:text="OK" 
    android:id="@+id/buttonok" 
    android:background="@drawable/buttonok"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-12-04 09:21

    Here is the solution

    <LinearLayout
        android:id="@+id/buttons_line1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageButton
            android:id="@+id/btn_mute"
            android:src="@drawable/btn_mute"
            android:background="@drawable/circle_gray"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
        <ImageButton
            android:id="@+id/btn_keypad"
            android:layout_marginLeft="50dp"
            android:src="@drawable/btn_dialpad"
            android:background="@drawable/circle_gray"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
        <ImageButton
            android:id="@+id/btn_speaker"
            android:layout_marginLeft="50dp"
            android:src="@drawable/btn_speaker"
            android:background="@drawable/circle_gray"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_below="@+id/buttons_line1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="mute"
            android:clickable="false"
            android:textAlignment="center"
            android:textColor="@color/Grey"
            android:layout_width="60dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="keypad"
            android:clickable="false"
            android:layout_marginLeft="50dp"
            android:textAlignment="center"
            android:textColor="@color/Grey"
            android:layout_width="60dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="speaker"
            android:clickable="false"
            android:layout_marginLeft="50dp"
            android:textAlignment="center"
            android:textColor="@color/Grey"
            android:layout_width="60dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题