我有一个图像列表,我需要一个接一个地显示在页面上,以及所有下面的文字。我在文件的xml中有LinearLayout,我添加了与该文本相关的部分(一些TextViews)。因为我需要调用服务来获取图像URL,所以我在代码中将图像添加到LinearLayout。我使用此代码使用其url获取图像并将其添加到布局中(在AsyncTask实现的onPostExecute部分中)。
/**
* Download image using image url and show it in ImageView
*/
private class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
public DownloadImageTask() {
super();
}
protected Bitmap doInBackground(Void... arg0) {
String urlDisplay = mDocumentUrls.get(mDocumentIdx);
mDocumentIdx = mDocumentIdx + 1;
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
// We add a new ImageView to the LinearLayout of the page and set it
// source to the downloaded image
ImageView newImageView = new ImageView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(convertDpToPx(20), convertDpToPx(5), convertDpToPx(20), 0);
newImageView.setImageBitmap(result);
mDocumentDetailsLayout.addView(newImageView, params);
//Next image
if(mDocumentIdx < mDocumentUrls.size())
new DownloadImageTask().execute();
}
}
这是页面的xml(与LinearLayout相关的部分)
<ScrollView
android:id="@+id/details_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/splashScreenGray
android:visibility="gone" >
<LinearLayout
android:id="@+id/details_form_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/splashScreenGray"
android:clickable="false"
android:orientation="vertical" ...
图片显示在页面上,但我有两个问题: 我无法横向居中(prtsc1.png) 2.当我的手机处于纵向模式时,我有这个边距(第一张图像的上边距和图像之间的边距),当我的手机处于纵向模式时我没有这种边距(我不需要它们) - 比较prtsc1。 png和prtsc2.png。
我尝试了一切,但我不确定我做错了什么。
答案 0 :(得分:0)
希望这可以帮助...让你的imageView得到集中统一......
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
任何问题仍然存在,请告诉我......
答案 1 :(得分:0)
我建议您尝试相对布局而不是LinearLayout。 LinearLayout有一个独立的大脑,否则它很好,但有时它可能搞砸了。
您应该更改图像的参数以使其自身在文本
下方对齐对于第二个,如果你使用的是linearlayout,你应该使宽度为match_parent和padding。