Padding funktioniert nicht auf ImageButton

Lesezeit: 3 Minuten

Benutzer-Avatar
Null

In einer App, an der ich arbeite, habe ich mehrere ImageButtons. Jeder ImageButton hat einen Hintergrund und Inhalt in Form eines Drawables. Im Moment hat das Drawable die maximale Größe innerhalb der Grenzen des ImageButton, aber ich möchte, dass es verkleinert wird, also muss ich etwas Polsterung hinzufügen. Die Sache ist, dass wenn ich das versuche, es keine Wirkung hat. Mein XML ist wie folgt für jeden ImageButton:

<ImageButton
    android:id="@+id/button_zero"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="10dip"
    android:src="https://stackoverflow.com/questions/20545742/@drawable/button_zero"
    android:background="@drawable/button_background" />

Irgendwelche Ideen, warum die Polsterung nichts tut?

Vollständiger XML-Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#222222"
    tools:context=".Main" >

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

        <!-- Row One -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <ImageButton
                android:id="@+id/button_zero"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="https://stackoverflow.com/questions/20545742/@drawable/button_zero"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_one"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_two"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_two"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_three"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_three"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_four"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_four"
                android:background="@drawable/button_front" />
        </LinearLayout>

        ... same for other rows

    </LinearLayout>

  • Können Sie bitte Ihren gesamten XML-Code hinzufügen?

    – Benutzer2235615

    12. Dezember 2013 um 14:14 Uhr

  • @ user2235615 Ich habe die Frage aktualisiert 🙂

    – Null

    12. Dezember 2013 um 14:20 Uhr

Benutzer-Avatar
Gooziec

Sie müssen Ihre ImageButton-Definition hinzufügen

android:scaleType="fitCenter"

oder ein anderer scaleType wie fitXY, da image standardmäßig versucht, so weit wie möglich zu skalieren und Padding zu ignorieren

  • Besser ist ScaleX=1,4 und ScaleX=1,4

    – Hrvoje Matić

    9. März 2017 um 13:47 Uhr

Die Polsterung wirkt sich nur auf die aus android:src Attribut, nicht auf der android:background.

Stellen Sie das erste auf Ihr Schaltflächenbild und das zweite auf ein android:background="@android:color/transparent"

  • Ich würde die Leute auffordern, zu verwenden ?attr/selectableItemBackground eher als transparent, damit das Touch-Feedback basierend auf der Themenkonfiguration intakt ist.

    – Trev Jonez

    6. Februar 2019 um 21:59 Uhr

Besser verwenden Bildansicht als ImageButton Wenn Sie das Bild mit kleiner einstellen müssen padding

dieses Thema über scaleType Standardmäßig im ImageButton fitXYdu musst dich nur ändern fitCenter.

<ImageButton
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:paddingVertical="4dp"
            android:scaleType="fitCenter" />

1175200cookie-checkPadding funktioniert nicht auf ImageButton

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy