ScrollView滚动布局使用原理:
①滚动产生的条件是,里面的内容大于物理尺寸
②ScrollView里面只有一个子元素,这个子元素就是一个线性布局LinearLayout,我们可以在线性布局中添加我们需要的内容,所以ScrollView中得包裹一层,并且线性布局中设计的方向必须纵向;再加任何其他的标签都是错误的,如果加标签,应该在LinearLayout里加
③不要把ScrollView和ListView放在一起用
第一种是静态布局如下:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_scrollview.MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView android:src="@drawable/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView android:src="@drawable/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView android:src="@drawable/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
package com.example.android_scrollview;
import com.example.android_scrollview.R.layout;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
//动态加载图片
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//scrollView=(ScrollView) this.findViewById(R.id.scrollView1);
linearLayout=(LinearLayout) this.findViewById(R.id.linearLayout1);
for(int i=0;i<10;i++){
ImageView imageView=new ImageView(this);
Drawable drawable=getResources().getDrawable(R.drawable.a);//加载图片
imageView.setImageDrawable(drawable);
linearLayout.addView(imageView, i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
布局效果图如下
案例:把新闻内容显示到手机上
在eclipse中创建一个ScrollView_web工程,在里面创建一个news.html,启动Servers服务器
在AndroidMainfest.xml中加上网络授权:<uses-permission android:name="android.permission.INTERNET"/>
方法:点击AndroidMainfest.xml中的Permission,点击Add,然后点击Uses Permission中找到permission.INTERNET
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_scrollview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
编写工具类HttpUtils.java
package com.example.android_scrollview.http;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpUtils {
/**
*
* @param path
* @param encoding
* @return
*/
public static String sendPostMethod(String path,String encoding){
String result="";
HttpClient httpClient=new DefaultHttpClient();
try {
HttpPost post=new HttpPost(path);
HttpResponse response=httpClient.execute(post);
if(response.getStatusLine().getStatusCode()==200){
result=EntityUtils.toString(response.getEntity(),encoding);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();
}
return result;
}
}
MainActivity.java
package com.example.android_scrollview;
import com.example.android_scrollview.http.HttpUtils;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
//动态加载图片
private LinearLayout linearLayout;
private ProgressDialog dialog;
private final String HTML_PATH="http://192.168.1.100:8080/scrollView_web/news.html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout) this.findViewById(R.id.linearLayout1);
dialog=new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("loading...");
new MyTask().execute(HTML_PATH);
}
//由于要访问网络,所以写一个线程
class MyTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result=HttpUtils.sendPostMethod(params[0], "utf-8");
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
TextView textView=new TextView(MainActivity.this);
Spanned spanned=Html.fromHtml(result);//过滤HTML标签
textView.setText(spanned);
textView.setMovementMethod(new LinkMovementMethod());//处理HTML中超链接的事件
linearLayout.addView(textView);
dialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
运行效果图