2016-10-26 6 views
0

Мне нужна помощь, пожалуйста, я не могу показать текст внизу или внизу на моем значке или изображении в ImageButton Я попытался использовать TextView, но он путает мою кнопку. У меня нет идеи, как для отображения текста. вот мой xml.текст не отображается в ImageButton со значком или изображением

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 


    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:orientation="horizontal" 
      android:background="#eeeeee" android:layout_marginTop="30dp"> 

     <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="2dp" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="30dp" 
       android:scaleType="fitCenter" 
       android:background="@drawable/button1_style" 
       android:src="@drawable/icon1_48dp" 
       style="@style/Widget.AppCompat.Button.Colored" /> 

     <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="2dp" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="30dp" 
       android:scaleType="fitCenter" 
       style="@style/Widget.AppCompat.Button.Colored" 
       android:background="@drawable/button2_style" 
       android:src="@drawable/icon2_48dp" 
       android:tint="#ffffff" 

       /> 

    </LinearLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:orientation="horizontal" 
      android:background="#eeeeee" android:layout_marginBottom="20dp"> 

    <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="2dp" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="30dp" 
       android:scaleType="fitCenter" 
       style="@style/Widget.AppCompat.Button.Colored" 
       android:src="@drawable/icon3_48dp" 
       android:background="@drawable/button3_style" 
       android:tint="#ffffff" 
       /> 

     <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="2dp" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="30dp" 
       android:scaleType="fitCenter" 
       style="@style/Widget.AppCompat.Button.Colored" 
       android:src="@drawable/icon4_48dp" 
       android:background="@drawable/button4_style" 
       /> 
    </LinearLayout> 

</LinearLayout> 
+0

текст не может показать в '' ImageButton'' http://stackoverflow.com/questions/21776676/set-the-text-in -imagebutton –

+0

Как применить TextView? – jemz

+0

вы хотите, чтобы кнопка была '' ImageButton'' или только '' Button''? –

ответ

4

Я думаю, что вы можете использовать TextView для своей идеи:

Пример для Button1:

<TextView 
    android:layout_width="wrap_content" 
    android:background="@drawable/button1_style" 
    android:drawableTop="@drawable/icon1_48dp" 
    android:text = "Button1" 
    android:layout_height="wrap_content"/> 

Добавить padding, margin, drawablePadding для него, если вам нужно.

+0

Поместим текстовое изображение после тега ImageButton? – jemz

+0

Нет, вы не используете ImageButton. TextView заменит ImageButton. – phongvan

+0

как я могу сделать значок больше, как этот размер http://stackoverflow.com/questions/40235609/gridview-with-button-and-text – jemz

1

Folllowing код для 4-х изображений и 4 текста под ними

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:orientation="horizontal" 
     android:background="#eeeeee" android:layout_marginTop="30dp"> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:padding="30dp" 
      android:scaleType="fitCenter" 
      android:background="@drawable/button1_style" 
      android:src="@drawable/icon1_48dp" 
      style="@style/Widget.AppCompat.Button.Colored" /> 


     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:padding="30dp" 
      android:scaleType="fitCenter" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:background="@drawable/button2_style" 
      android:src="@drawable/icon2_48dp" 
      android:tint="#ffffff" 

      /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/TextViewlayout1" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="2" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:text="Something 1" 
      android:gravity="center" 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:text="Something 2" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:orientation="horizontal" 
     android:background="#eeeeee" > 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:padding="30dp" 
      android:scaleType="fitCenter" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:src="@drawable/icon3_48dp" 
      android:background="@drawable/button3_style" 
      android:tint="#ffffff" 
      /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:padding="30dp" 
      android:scaleType="fitCenter" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:src="@drawable/icon4_48dp" 
      android:background="@drawable/button4_style" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/TextViewlayout2" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="2" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:text="Something 3" 
      android:gravity="center" 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:text="Something 4" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</LinearLayout> 
+0

текст снаружи в кнопке – jemz