其實我已經完成了「從網址加載」部分,但結果並不令人滿意。Android如何從網址加載圖片並將其放在頂部
我將圖像放置在頂部。但是,當它加載圖像時,它會自動對齊到中心,但我已聲明android:layout_alignParentTop="true"
。
此外,我想有形象上的一些文本,但文本總是在左上角..........
這裏是我的代碼: (XML)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/white" />
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/product_image"
android:text="@string/hello_world" />
<TextView
android:id="@+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/product_price"
android:text="@string/hello_world" />
</RelativeLayout>
活動:
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class PostDetail extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
WPTemplateDB dpOpener = new WPTemplateDB(this);
SQLiteDatabase db = dpOpener.getReadableDatabase();
//Log.d("passed pid", getIntent().getStringExtra("pid")+"");
Cursor cursor = db.query(WPTemplateDB.PRODUCT_TABLE,
new String[]{WPTemplateDB.TITLE, WPTemplateDB.IMAGE, WPTemplateDB.PRICE},
WPTemplateDB.PRODUCT_ID+"=?",
new String[]{getIntent().getStringExtra("pid")},
null, null, null);
String title = "", price = "", img = "";
while (cursor.moveToNext()){
title = cursor.getString(0);
img = cursor.getString(1);
price = cursor.getString(2);
}
Log.d("title", title);
Log.d("img_src", img);
TextView productTitle = (TextView)findViewById(R.id.product_title);
ImageView productImage = (ImageView)findViewById(R.id.product_image);
TextView productPrice = (TextView)findViewById(R.id.product_price);
new LoadImageFromURL(productImage).execute(img);
productTitle.setText(Html.fromHtml(title),TextView.BufferType.SPANNABLE);
productPrice.setText(price);
}
private class LoadImageFromURL extends AsyncTask<String, Void, Bitmap>{
ImageView bitmapImgView;
public LoadImageFromURL(ImageView bmImgView){
bitmapImgView = bmImgView;
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
String urlStr = params[0];
Bitmap img = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlStr);
HttpResponse response;
try {
response = (HttpResponse)client.execute(request);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
InputStream inputStream = bufferedEntity.getContent();
img = BitmapFactory.decodeStream(inputStream);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
@Override
protected void onPostExecute(Bitmap bitmap){
bitmapImgView.setImageBitmap(bitmap);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_post, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this,
new Intent(this, ListPost.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
您已聲明圖像視圖寬度爲android:layout_width =「match_parent」。更改爲fill_parent ..並嘗試,它可能工作。 –
@Brinda:圖片仍然位於屏幕中間:( – Simon
@ Brinda-user1594986:match_parent和fill_parent是一樣的 –