如何实现在一个ScrollView里添加多个ListView,并且让ListView伸张到最长。
用最外层ScrollView做滚动条。ListView本身不滚动。
全世界最简单的办法:
复写ListView里面的onMeasure方法,设置让其不滚动!
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }
1.户可以根据自己输入的地址下载网络上的图片
第一步:
先写layout的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示下载的图片" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示下载的html" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第二步再写activity
package com.hbsi.csdn.picture;
import com.hsbi.csdn.service.ImageService;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageview=(ImageView) findViewById(R.id.imageView1);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String path="http://list.image.baidu.com/t/image/w_bizhi.jpg";
try {
byte[] data=ImageService.getImage(path);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "出错啦", 1);
Log.e("ERR",e.toString());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第三步:
单独写一个imageservice类
package com.hsbi.csdn.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageService {
public static byte[] getImage(String path) throws Exception{
URL url=new URL(/blog_article/path/index.html);//得到代表路径的url;
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(20*1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
InputStream in=conn.getInputStream();
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=in.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
byte[] data=outStream.toByteArray();//图片的二进制数组
return data;
}
}
最后一步:
在清单文件中获得网络权限
<uses-permission android:name="android.permission.INTERNET"/>
想你了怎么办