2013-08-12 117 views
1

我试着像这样的代码如何在xml文件中显示文本旁边的图标?

<ImageView android:id="@+id/ivIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_btn_speak_now" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item0" /> 

但它输出这样的。这不是在同一条线上:(

enter image description here

我的整个XML代码就是这个样子。 (icon) item将是一个集,这将被用户点击到的活动而移动。
所以我需要分组在一起。

<LinearLayout style="@style/behindMenuScrollContent" 
    android:paddingTop="25dp" > 

    <TextView 
     style="@style/behindMenuItemTitle" 
     android:text="Subject" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item0" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item1" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item2" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item3" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item4" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item5" /> 

    <TextView 
     style="@style/behindMenuItemTitle" 
     android:text="Subject2" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item6" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item7" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item8" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item9" /> 

    <TextView 
     style="@style/behindMenuItemLabel" 
     android:text="Item10" /> 


    <Button 
     android:id="@+id/behind_btn" 
     style="@style/behindMenuItemLabel" 
     android:text="BUTTON" /> 

</LinearLayout> 

更新:我不需要图标和文本之间的余量,但只需要左边的余量!

enter image description here

style.xml

<style name="behindMenuItemLabel"> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_marginLeft">20dp</item> 
    <item name="android:layout_marginBottom">4dp</item> 
    <item name="android:textSize">30sp</item> 
    <item name="android:textColor">#d2d2d2</item> 
</style> 

回答

3

尝试是这样的:

<TextView 
    android:id="@+id/behindMenuItemLabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:layout_weight="40" 
    android:drawablePadding="-5dp" 
    android:gravity="center_vertical" 
    android:text="Speak Now" 
    android:textColor="#000000" 
    style="@style/behindMenuItemLabel" /> 

这是你的style.xml

<style name="behindMenuItemLabel"> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_marginLeft">20dp</item> 
    <item name="android:layout_marginBottom">4dp</item> 
    <item name="android:textSize">30sp</item> 
    <item name="android:drawableLeft">@drawable/your_icon</item> 
    <item name="android:textColor">#d2d2d2</item> 
</style> 
+0

谢谢。你看起来是我期待的那个。你如何用Style.xml来做这件事? – MKK

+0

有什么风格(开销)它更快,更可靠的方式来做到这一点。如果这为你工作,然后接受这个答案 –

+0

我会的!还有一件事我需要你的是如何使用style.xml来做到这一点,我必须在XML中多次使用它,所以我希望它作为样式。请参阅我的UPDATE问题!并告诉我如何修改我的style.xml请定制我的原始style.xml – MKK

4

您的需要进行充分利用的TextView的绘制属性填充,下面我给个例子:

<TextView 
     android:id="@+id/bookTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:drawableBottom="@drawable/icon" 
     android:text="Item 0" 
     style="@style/behindMenuItemLabel"/> 

希望它可以帮助

2

你可以在TextView

就在所有的四个侧面文本的设置图标:

android:drawableRight="@drawable/icon" 

上:

android:drawableTop="@drawable/icon" 

底:

android:drawableBottom="@drawable/icon" 

左:

android:drawableLeft="@drawable/icon"

+0

感谢它显示。但似乎图标和文字之间的边距相同。我怎样才能保持marginLeft禁用? – MKK

2

不要使用单独的ImageView的图标。 在你的TextView设置在右侧的绘制(或左,上,下)

android:drawableRight="@android:drawable/ic_btn_speak_now" 
+0

感谢它显示。但似乎图标和文字之间的边距相同。我怎样才能保持marginLeft禁用? – MKK

+0

你想减少保证金吗? –

+0

是的,只是图标和文字之间的边距 – MKK

1

您可以使用此ATTR android:drawableLeft="your_drawable_id"设置左文本的图像,这样的XML标签将是这样的:

<TextView 
    android:id="@+id/bookTitle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center_horizontal" 
    android:drawableLeft="@drawable/icon" 
    android:text="Item 0" 
    style="@style/behindMenuItemLabel"/> 

您还可以设置图像的权利,顶部或底部。最好的祝愿。

+0

y离开了,他需要我想。 –

相关问题