2013-08-12 113 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離開了,他需要我想。 –

相關問題