利用ScrollView滚动视图,显示一片文章的全部内容!
ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显示的视图。ScrollView只支持垂直滚动。
效果如下:
1.MainActivity 的界面如下:
2.点击按钮《你不懂我,我不怪你》后,显示的TestActivity界面。
3.下拉滚动条,显示全部文章的内容。
4.实例源码如下:
(1)TestActivity页面程序
other_activity.xml布局文件:(对应于TestActivity这个页面的布局)
<strong><span style="color:#ff0000;"><ScrollView 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:scrollbars="vertical"
android:background="@color/background_color"
>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></span></strong>
<TextView
android:id="@+id/mytextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
/>
<!-- android:gravity="center_horizontal" -->
<!-- android:textSize="16sp" -->
/>
<strong><span style="color:#ff0000;"> </LinearLayout>
</ScrollView></span></strong>
TestActivity源码:
package com.example.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TestActivity extends Activity {
private TextView mytextview1;
private final String moyan=
// 《你不懂我,我不怪你》
// 作者:莫言
"每个人都有一个死角,\n"
+"自己走不出来,别人也闯不进去。\n"
+"我把最深沉的秘密放在那里。\n"
+"你不懂我,我不怪你。\n\n"
+"每个人都有一道伤口,\n"
+"或深或浅,盖上布,以为不存在。\n"
+"我把最殷红的鲜血涂在那里。\n"
+"你不懂我,我不怪你。\n\n"
+"每个人都有一场爱恋,\n"
+"用心、用情、用力,感动也感伤。\n"
+"我把最炙热的心情藏在那里。\n"
+"你不懂我,我不怪你。\n\n"
+"每个人都有一行眼泪,\n"
+"喝下的冰冷的水,酝酿成的热泪。\n"
+"我把最心酸的委屈汇在那里。\n"
+"你不懂我,我不怪你。\n\n"
+"每个人都有一段告白,\n"
+"忐忑、不安,却饱含真心和勇气。\n"
+"我把最抒情的语言用在那里。\n"
+"你不懂我,我不怪你。\n\n"
+"你永远也看不见我最爱你的时候,\n"
+"因为我只有在看不见你的时候,才最爱你。\n"
+"同样,你永远也看不见我最寂寞的时候,\n"
+"因为我只有在你看不见我的时候,才最寂寞。\n\n"
+"也许,我太会隐藏自己的悲伤。\n"
+"也许,我太会安慰自己的伤痕。\n"
+"也许,你眼中的我,太会照顾自己,\n"
+"所以,你从不考虑我的感受。\n\n"
+"你以为,我可以很迅速的恢复过来,\n"
+"有些自私的以为。\n\n"
+"从阴雨走到艳阳,我路过泥泞、路过风。\n"
+"一路走来,你不曾懂我,我亦 不曾怪你。\n\n"
+"我不是为了显示自己的大度,\n"
+"也不是为了体现自己的大方。\n"
+"只想你让我知道,感情不在,责备也不存在。\n\n" ;
private final String text="明月几时有? 把酒问青天。\n\n"
+"不知天上宫阙,今夕是何年。\n\n"
+"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n\n"
+"起舞弄清影,何似在人间。 \n\n"
+"转朱阁,低绮户,照无眠。\n\n"
+"不应有恨,何事长向别时圆?\n\n"
+"人有悲欢离合,月有阴晴圆缺,此事古难全。\n\n"
+"但愿人长久,千里共婵娟。\n";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
mytextview1 = (TextView)findViewById(R.id.mytextView);
// mytextview1.setText(text);
mytextview1.setText(moyan);
}
}
</pre><pre code_snippet_id="379508" snippet_file_name="blog_20140606_2_6697922" name="code" class="html">(2)<span style="font-weight: bold;">MainActivit页面程序</span>
<span style="color:#ff0000;"><span style="font-weight: bold;"><span style="font-weight: bold;">activity_</span></span><span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif;">main</span><span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif;">.xml布局文件:(对应于</span><span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif;">MainActivity 这个页面的布局)</span></span>
<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=".MainActivity"
android:orientation="vertical"
android:background="@color/background_color"
>
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:src="@drawable/logo" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/logo"
android:layout_alignRight="@+id/logo"
android:layout_below="@+id/logo"
android:layout_marginTop="68dp"
android:text="你不懂我,我不怪你!" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="69dp"
android:text="" />
</RelativeLayout>
MainActivity 源码(启动代码,负责启动第一个Activity):
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.widget.TextView;
//import android.view.View.OnClickListener;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
//增加事件响应
//当有按键按下时,显示 <span style="font-family: Arial, Helvetica, sans-serif;">TestActivity中的文章</span>
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent =new Intent();
intent.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(intent); //调用<span style="font-weight: bold;">TestActivity</span>
}
});
}
@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;
}
}