9

ボタンに画像を配置する方法を考えていました。

「android:icon="@drawable/search.png」を使用しようとしましたが、この画像を drawable-hdpi フォルダーに追加しましたが、画像が表示されません。nexus 4 用のアプリケーションを開発しているので、 「画像のサイズがわかりません。追加しようとしている画像は、nexus 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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_group_coworkers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coworkers"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <TextView 
        android:id="@+id/lbl_group_family"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Family"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>
    <TextView
        android:id="@+id/lbl_group_friends"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Friends"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical" >


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.70"
            android:icon="@drawable/search.png" />

        <Button
            android:id="@+id/Button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="addGroup"
            android:icon="@drawable/addgroup.png"/>

        <Button
            android:id="@+id/Button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.11"
            android:text="@string/Button3" />
    </LinearLayout>

</LinearLayout>
4

4 に答える 4

36
 <Button
        android:id="@+id/Button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="addGroup"
        android:text="TEXT"
        android:background="@drawable/addgroup"/>

backgroundimagetext同時に追加します。

交換するだけ

android:background="@drawable/addgroup"

android:background="@android:color/transparent"
android:drawableTop="@drawable/addgroup"
android:text="TEXT"

Buttonボタン内に配置する画像ソースを定義するプロパティを使用して、レイアウトで属性を使用することもできます

android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop
于 2013-09-13T07:26:32.920 に答える
0

属性を使用する

android:background="@drawable/...."
于 2013-09-13T07:25:12.353 に答える
0

以下を使用して、ボタンの背景を設定できます。

android:background="@drawable/..."

于 2013-09-13T07:26:28.003 に答える