在 GridView 中自动缩放图像
Auto-scale an image in GridView
我有一个简单的网格视图,左侧有一个图像,图像旁边有两行文本。图片的"rowspan"为2是为了使用两行文字的全高:
<?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>
我的问题:我想将图像自动缩放到适合两行文本高度的大小,而不是以原始高度显示。有没有办法让布局自动执行此操作?
谢谢!
<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);
我有一个简单的网格视图,左侧有一个图像,图像旁边有两行文本。图片的"rowspan"为2是为了使用两行文字的全高:
<?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>
我的问题:我想将图像自动缩放到适合两行文本高度的大小,而不是以原始高度显示。有没有办法让布局自动执行此操作?
谢谢!
<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);