Android之ScrollView滾動佈局控制元件使用以及顯示新聞網頁
阿新 • • 發佈:2019-01-03
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);
}
}
執行效果圖