Android 回到底部和返回顶部实现

效果

Android 回到底部和返回顶部实现

准备四张图片资源

Android 回到底部和返回顶部实现

首先XML布局

我们采用FrameLayout布局 
代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ad1" /> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="@drawable/ad2" /> </LinearLayout>
</ScrollView> <ImageView
android:id="@+id/btn_toTop"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="bottom|right"
android:background="@drawable/top" /> <ImageView
android:id="@+id/btn_toBottom"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/bottom" /> </FrameLayout>

其次Activity里面实现

代码:

package com.jabony.fastscrolltop;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ScrollView; public class MainActivity extends Activity {
private ImageView topBtn, bottomBtn;
private ScrollView sc; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sc = (ScrollView) findViewById(R.id.sc);
topBtn = (ImageView) findViewById(R.id.btn_toTop);
bottomBtn = (ImageView) findViewById(R.id.btn_toBottom);
topBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sc.post(new Runnable() { @Override
public void run() {
sc.post(new Runnable() {
public void run() {
// 滚动至顶部
sc.fullScroll(ScrollView.FOCUS_UP);
}
});
}
}); }
});
bottomBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sc.post(new Runnable() { @Override
public void run() {
sc.post(new Runnable() {
public void run() {
// 滚动到底部
sc.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
}
}); } }

最后运行就可以了。

源码下载地址: 
http://download.csdn.net/detail/care_about/9481210

上一篇:PHP hexdec() 函数


下一篇:hdu----(5045)Contest(数位dp)