先看布局:
Scroll 解釋: 滾動 文本框中的起始滾動行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="test.pgl.com.scrolltext.MainActivity">
<Button
android:text="scrollto"
android:onClick="scrollto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="scrollby"
android:onClick="scrollby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_text"
android:background="#ff00"
android:textColor="#00ff00"
android:layout_width="300dp"
android:layout_height="300dp"
android:text="Hello World!" />
</LinearLayout>
ScrollTo 和 ScrollBy
共同點 都可以移動View的內容 傳入負數(shù)向右yido
ScrollTo傳的是坐標點的值 scrollBy移動的距離
package test.pgl.com.scrolltext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = (TextView) findViewById(R.id.tv_text);
}
public void scrollby(View view) {
tv_text.scrollBy(-40,0);
}
public void scrollto(View view) {
tv_text.scrollTo(0,-100);
}
}
這里要注意一下:
當坐標為"-40"負的時會出現(xiàn)下面這種: 但是這沒影響
Paste_Image.png
還有就是: 這里的坐標要寫負的才會向x,y正方向移動,這和平時的相反
運行:
Paste_Image.png
點擊scrollto:
Paste_Image.png
點擊scrollby:
Paste_Image.png
再點擊scrollby:
Paste_Image.png
每點擊一次scrollby就會向右移動 直到移出
這時點擊scrollto:
Paste_Image.png
將回到 指定的位置:
public void scrollto(View view) {
tv_text.scrollTo(0,-100);
}