-1

Ich habe eine einfache Rasteransicht mit einem Bild auf der linken Seite und zwei Textzeilen rechts neben dem Bild. Die „rowspan“ des Bildes ist 2, um die volle Höhe der beiden Textzeilen zu verwenden:Automatisches Skalieren eines Bildes in GridView

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:rowCount="2" 
    android:columnCount="2"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="0" 
    android:layout_rowSpan="2" 
    android:src="@drawable/disconnected" /> 

<TextView 
    android:id="@+id/connectionText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:layout_column="1" 
    android:layout_row="0" 
    android:layout_rowSpan="1" 
    android:inputType="textPersonName" 
    android:text="Name" /> 

<TextView 
    android:id="@+id/stateText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:layout_column="1" 
    android:layout_row="1" 
    android:layout_rowSpan="1" 
    android:inputType="textPersonName" 
    android:text="disconnected" /> 
</GridLayout> 

Mein Problem: Ich möchte das Bild auf eine Größe Auto-Skala, die auf die Höhe passt der zwei Textzeilen, anstatt mit seiner ursprünglichen Höhe zu erscheinen. Gibt es eine Möglichkeit, das Layout automatisch ausführen zu lassen?

Danke!

Antwort

3
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:rowCount="2" 
     android:columnCount="2"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:scaleType="fitCenter" 
     android:layout_rowSpan="2" 
     android:src="@drawable/disconnected" /> 

    <TextView 
     android:id="@+id/connectionText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:layout_rowSpan="1" 
     android:inputType="textPersonName" 
     android:text="Name" /> 

    <TextView 
     android:id="@+id/stateText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:layout_rowSpan="1" 
     android:inputType="textPersonName" 
     android:text="disconnected" /> 
</GridLayout> 

Ich habe Ihre XML geändert, kann es helfen. Ich habe einen bestimmten Wert für Breite und Höhe angegeben und den Maßstabstyp passend zum Mittelpunkt festgelegt.

Sie können durch die Programmierung dynamischer Höhe eingestellt ist, wird dieser Code hilft Ihnen

 int first_view_hight = firstView.getLineHeight(); 


     int second_view_height = secondView.getLineHeight(); 


     int totalHeight = first_view_hight + second_view_height; 


     GridLayout.LayoutParams lp = (GridLayout.LayoutParams) setHeightImage.getLayoutParams(); 
     lp.height = totalHeight; 
     lp.width = totalHeight; 
     imageView.setLayoutParams(lp); 
+0

Hm, aber diese Waage auf eine feste Größe von 40x40 dp und nicht dynamisch an die reale Größe der beiden Textzeilen rechts neben es!? – Elmi

+0

Ich habe meine Antwort aktualisiert, es kann Ihnen helfen – RajatN

Verwandte Themen