您的位置:首页 > 移动开发 > Android开发

android *** Layout 13 ScrollView

2016-02-02 20:00 549 查看
http://android-doc.com/reference/android/widget/ScrollView.html

网址如下,ScrollView视图主要是给用户一个滚动的窗口,然后ScrollView里面只有一个LinearLayout的 布局,且只能有这一个,其余其他的内容只能在linearLayout里面添加。

再有就是可以直接在xml下的文件里给linearLayout添加元素,这样的话就导致所有的内容都是死的,已经固定了。同时也可以在linearLayout里面通过.addView来添加内容,以此来得到实时的更新。

其xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<ScrollView
android1:id="@+id/scrollView1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentLeft="true"
android1:layout_alignParentRight="true"
android1:layout_alignParentTop="true" >

<LinearLayout
android1:id="@+id/line"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:orientation="vertical" >
<--!                                                                                                                                如果要定义死的内容,就在这里面定义内容                                                                                              -->
</LinearLayout>
</ScrollView>

</RelativeLayout>


如果从一个网站里面下载内容下来,那么可以用以下方法,xml文件如上
其java内容如下
package com.example.tree;

import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.example.tree.internet.HttpUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private LinearLayout linear;
private ProgressDialog dialog;
private final String TAG="MainActivity";
private final String HTML_PATH="http://m.baidu.com/?from=1014711l#";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "-onCreate-->>");
dialog=new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("loading...");
linear=(LinearLayout)this.findViewById(R.id.line);
new MyTask().execute(HTML_PATH);
}

class MyTask extends AsyncTask<String,Void,String>{

protected void onPreExecute(){
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;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
Spanned spanned=Html.fromHtml(result);
TextView textView=new TextView(MainActivity.this);
textView.setText(spanned);
textView.setMovementMethod(new LinkMovementMethod());
linear.addView(textView);
dialog.dismiss();
}
}

protected void onStart(){
super.onStart();
Log.i(TAG, "-onStart-->>");
}

protected void onRestart(){
super.onRestart();
Log.i(TAG, "-onRestart-->>");
}

protected void onResume(){
super.onResume();
Log.i(TAG, "-onResume-->>");
}

protected void onPause(){
super.onPause();
Log.i(TAG, "-onPause-->>");
}

protected void onStop(){
super.onStop();
Log.i(TAG, "-onStop-->>");
}

protected void onDestroy(){
super.onDestroy();
Log.i(TAG, "-onDestroy-->>");
}

@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);
}
}


用ProgressDialog类实例化一个dialog类,并且使用dialog.show();和dialog.dismiss();来实现对话框的显示和关闭

同时

//此段来自引用

①在onPostExecute方法中,需要使用

Spanned spanned=Html.fromHtml(result);

textView.setText(spanned);

从html页面中连带html标签获取到客户端中;

②并在客户端中需要处理HTML中超链接的事件,所以需要用到

textView.setMovementMethod(new LinkMovementMethod());

这样就可以在手机客户端中点击页面中的超链接了。


同时

因为要获取网络上的数据,即网页数据,所以需要另外一个工具类HttpUtils,在这里贡献出来,代码如下:
package com.example.tree.internet;

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 {

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) {
e.printStackTrace();
} finally{
httpClient.getConnectionManager().shutdown();
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: