2013-04-26 6 views
20

CompoundDrawableはイメージとテキストが正しく表示されているTextViewですか?だから2つの組み合わせであれば、イメージを中心にテキストをどのように置くことができますか?または、テキストが画像に比べて少し小さければ、テキストを中心に画像をどのように置くことができますか?または、イメージがテキストよりも小さい場合はどうなりますか? XMLを使用してこれらのシナリオをどのようにカバーできますか?ここでCompoundDrawableにテキストとイメージを配置するにはどうすればいいですか?

はCompoundDrawableの例である:

<TextView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/image" 
    android:text="@string/text" /> 

答えて

38

は、あなたのTextViewにandroid:gravity="center"を追加します。

+0

それはyou.one私は画像またはその逆の側にテキストを置くことができますどのように多くの感謝そのことについて申し訳ありませんでしたか? – Giant

+2

@HakHak、android:drawableLeft、android:drawableRightなどを使用して、別の場所に画像を配置します。 – andyandy

1
<TextView 
    android:id="@+id/filterView" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:background="@drawable/background_card" 
    android:text="Offers" 
    android:drawableLeft="@drawable/small_badge_dollar" 
    android:drawablePadding="-20dp" 
    android:paddingLeft="20dp" 
    android:textColor="@color/offers_color"`enter code here` 
    android:textSize="25sp" 
    android:textStyle="bold" /> 
+0

android:drawablePadding = " - 20dp" アンドロイド:paddingLeft = "20dp" – yesh

1
cool stuff with android:drawablePadding="-20dp" android:paddingLeft="20dp" 

    <Button 
     android:id="@+id/btn_pre" 
     style="?android:buttonBarStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginRight="1dp" 
     android:layout_weight="1" 
     android:background="@color/bottom_tab" 
     android:contentDescription="@string/image_view" 
     android:drawableLeft="@drawable/prev" 
     android:drawablePadding="-20dp" 
     android:onClick="navigateStaticPages" 
     android:paddingLeft="20dp" 
     android:text="@string/pre" 
     android:textColor="#000" 
     android:textSize="@dimen/login_textSize" /> 
+0

パディングに負の値を入れるのは良い方法ですか? – Giant

+1

私のレイアウトで使用していますが、私は何の欠点も見ませんでした。あなたの要件に依存します。 –

+0

私のtextviewが親(0dp)にマッチすれば? – Subho

関連する問題