Android-来自web服务的图像在不同的设备上有不同的大小
我知道关于这个话题有很多问题。但我仍然需要帮助。我想在我的Android-来自web服务的图像在不同的设备上有不同的大小,android,imageview,resolution,tablelayout,nine-patch,Android,Imageview,Resolution,Tablelayout,Nine Patch,我知道关于这个话题有很多问题。但我仍然需要帮助。我想在我的表格布局中显示我的图像视图具有相同的宽度和高度。但在不同的设备上,我无法做到这一点 下面是我使用AsyncTask中的URL获取图像,并将ImageView设置为ononPostExecute方法 protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try {
表格布局中显示我的图像视图
具有相同的宽度
和高度
。但在不同的设备上,我无法做到这一点
下面是我使用AsyncTask
中的URL
获取图像,并将ImageView
设置为ononPostExecute
方法
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// set the imageview
bmImage.setImageBitmap(result);
}
这是我的XML文件(news.XML),我的ImageView位于这里
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:paddingBottom="10dp"
android:id="@+id/mylayout"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:clickable="true"
android:background="@drawable/border"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:maxLines="4"
android:id="@+id/text"
android:layout_below="@+id/image"
android:layout_alignEnd="@+id/image"
android:layout_alignRight="@+id/image"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
我在手机上得到了完美的结果。但是在屏幕大小不同的设备上,ImageView
在顶部和底部都有空格。我试图转换九块补丁,并将其再次转换为位图,但并没有区别。如果我无法从web服务获取图像,我会将其他分辨率图像放入mipmap hpdi
,mipmap mdpi
等文件夹中,并且不会出现错误。但是我是动态获取图像的,那么如何才能实现ImageView
屏幕宽度的一半呢?
提前谢谢。这里我已经编辑了您的xml
尝试使用权重线性布局,根据需要更改权重,更改布局权重,使图像视图和文本视图变小或变大
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="vertical"
android:id="@+id/mylayout"
android:weightSum="10">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:id="@+id/image"
android:src="@drawable/sd"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:maxLines="4"
android:id="@+id/text"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</LinearLayout>
我在这里编辑了您的xml
尝试使用权重线性布局,根据需要更改权重,更改布局权重,使图像视图和文本视图变小或变大
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="vertical"
android:id="@+id/mylayout"
android:weightSum="10">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:id="@+id/image"
android:src="@drawable/sd"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:maxLines="4"
android:id="@+id/text"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</LinearLayout>
您的版面中是否只有两件事。是的,在my news.xml中,您可以在问题中看到,相对版面中有一个图像视图和一个文本视图。您的版面中是否只有两件事。是的,在my news.xml中,相对版面中有一个图像视图和一个文本视图,您可以在问题中看到。仍然图像视图有很多在位图的顶部和底部添加空格以使用位图。createScaledBitmap();android:fitsystemwindows=“true”在您的imageview中,Still imageview的顶部和底部都有很多空间来使用您的位图;和android:fitsystemwindows=“true”在您的imageview中