2017-09-05 61 views
-1

我有一个简单的网格视图,左边有一个图像,图像旁边有两行文本。该图像的“行跨度”是2,以使用文本的两行的全高:在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> 

我的问题:我想自动缩放图像的尺寸适合的高度的两行文字,而不是出现在它的原生高度。有没有办法让Layout自动执行?

谢谢!

回答

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> 

我修改了你的xml,它可能有帮助。 我已经给出了具体的宽度和高度值,并设置缩放类型以适合中心。

您可以设置通过编程动态的高度,这个代码将帮助您

 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

嗯,但这扩展到40×40 DP的固定大小,而不是动态文本的两行的真实尺寸旁边它!? – Elmi

+0

我已经更新了我的答案,可能对您有所帮助 – RajatN