2012-03-24 8 views
6

In meinem horizontalen LinearLayout habe ich einen TextEdit und einen ImageButton. Der ImageButton ist so hoch wie der TextEdit.ImageButton: Quadratisches Symbol erzwingen (Höhe = WRAP_CONTENT, Breite =?)

Ich möchte, dass der ImageButton genau so breit wie lang ist.

Im Moment ist es wie die Breite des Image aussieht, ist wie wenn keine Skalierung gibt es (Image Breite [Pixel] = unscaled ziehbar Breite [px]):

example

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

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

</LinearLayout> 

Wie es sollte wie folgt aussehen:

objective

+1

Nur ein kleiner Tipp verwenden verwenden. Wenn Sie eine layout_weight für eine Ansicht festlegen, ist es beim Rendern schneller, die Breite auf 0 dpi festzulegen. –

+0

@BananaNutTrüffel: Danke! Ich wusste nichts davon. – SecStone

+0

Haben Sie eine Lösung gefunden? – rekire

Antwort

-3

Sie die Schaltfläche Bild Pixel mit der Größe verändern können ... wie unten ...

`android:layout_width="5px" 
    android:layout_height="5px"` 
+0

ich bin ziemlich sicher, er will es skalierbar –

+0

Genau;) (Allerdings ist es nicht in der Problemstellung angegeben.) – SecStone

0

Verwenden Sie einfach den weightSum die Größe der Steuerelemente entsprechend devide ...

<?xml version="1.0" encoding="utf-8" ?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1.0" > 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_weight="0.8" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:inputType="text" 
     android:singleLine="true"/> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_weight="0.2" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

    </LinearLayout> 
</LinearLayout> 

Hoffe, es wird Ihnen helfen.

+0

Vielen Dank für Ihren Vorschlag. Wenn die App auf einem kleinen Bildschirm läuft, benötige ich vielleicht 25% oder 30% des Platzes, um die Schaltfläche als Quadrat darzustellen. Gibt es auch eine Lösung für dieses Problem? – SecStone

2

Verwenden

android:scaleType="fitCenter" 

oder android:scaleType="centerInside"

in der Image in XML-Datei.

+0

Danke für den Vorschlag. Welche Werte sollte ich für 'android: layout_width' und' android: layout_height' verwenden? – SecStone

+0

können Sie füllen Eltern oder wrap Inhalt nach Ihrer Anforderung ... !!! Und bitte überprüfen Sie die Antwort, wenn Sie zufrieden waren ... !!! –

19

die Sie interessieren, ich denke, das sollte funktionieren:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:orientation="horizontal" > 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     android:src="@drawable/pin2" /> 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@id/btSet" /> 

</RelativeLayout> 

Explaination:centerInside wird sicherstellen, dass das Bild proportional innerhalb der Grenzen des ImageButton skaliert. adjustViewBounds="true" wird ... nun, die Grenzen der Ansicht anpassen, wenn das Bild skaliert werden muss.

+0

Danke für den Vorschlag. Leider funktioniert es nicht richtig (Screenshot: http://i.imgur.com/W8iFu.png). Kennst du eine andere Lösung? – SecStone

+0

Ich habe meine Antwort bearbeitet, probiere das aus. Kannst du auch die von dir verwendete Schaltfläche hochladen (falls das nicht funktioniert)? –

+0

Danke für die aktualisierte Antwort. Es scheint noch nicht zu funktionieren (Screenshot: http://i.imgur.com/Awbat.png). Mein Zeichensatz (_pin2_): http://i.imgur.com/eflnh.png – SecStone

5

try

adjustViewBounds = "true"

zum Image fügte hinzu, dass die überschüssige Breite


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

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/pin" /> 
</LinearLayout> 
+0

Danke für den Vorschlag. Leider funktioniert es nicht richtig (Screenshot: http://i.imgur.com/W8iFu.png). Kennst du eine andere Lösung? – SecStone

+0

Versuchen Sie, Tag scaleType zu entfernen und legen Sie layout_width und height auf wrap_content –

+0

Haben Sie es versucht, Sir? Ich habe es an meinem Ende versucht und es hat funktioniert. –

0
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="1.0" > 

<EditText 
    android:id="@+id/txtName" 
    android:layout_weight="0.75" // this work like percentage adjust as u want 70 or 75 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:inputType="text" 
    android:singleLine="true"/> 

<ImageButton 
    android:id="@+id/btSet" 
    android:layout_weight="0.25" // this work like percentage adjust as u want 25 or 30 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:scaleType="fitEnd" 
    android:src="@drawable/pin2" /> 

</LinearLayout> 
0

einfach die folgende Zeile in die Image hinzufügen und die zusätzlichen Hintergrund verblassen weg:

android:layout_gravity="center|clip_horizontal" 

Hoffnung das funktioniert

0

lief einfach in so etwas wie die anderen Vorschläge nicht helfen. Was tat Hilfe wurde in der Image die Polsterung Einstellung:

android:padding="5dp" 

Abgesehen davon, habe ich nicht das ursprüngliche Layout berühren. Der Knopf wurde Platz auf den 3-Emulatoren, die ich getestet (3.2, 4.4.2, 5.1)

0

versuchte ich mich, es funktioniert

einfach tun, um die folgende ...

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

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="0dp" 
     android:layout_weight="5" 
     android:layout_height="wrap_content" 
     android:inputType="text"/> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

Verwendung Skizze Gewichtsverhältnis von 5: 1 in einem LinearLayout für EditText und ImageButton

OUTPUT

1

ich könnte sein, ein bisschen spät zur Party. Es gibt jedoch eine einfache Möglichkeit, dieses Verhalten zu erreichen, indem onMeasure() überschrieben wird. Hier ist, wie es aussehen würde:

public class MySquareImageButton extends ImageButton { 
    public MySquareImageButton(Context context) { 
     super(context); 
    } 

    public MySquareImageButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MySquareImageButton(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     //(1)if you want the height to match the width 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
     //(2)if you want the width to match the height 
     //super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
    } 
} 

Und dann würden Sie einfach Ihre XML die Image mit diesem Brauch ersetzen ein:

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

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <com.whatever_your_package.MySquareImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

</LinearLayout> 

Sie einfach wrap_content setzen würde oder Höhe Breite, je auf welchem ​​man die Größe des Knopfes diktieren möchte. Im Fall, dass Sie möchten, dass Ihre Schaltfläche seine Höhe auf das Bild wickeln, und dass die Breite einfach die Höhe passt, dann würden Sie

 android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

und

 //(2)if you want the width to match the height 
     //super.onMeasure(heightMeasureSpec, heightMeasureSpec);