如何控制ScrollView的滚动条,让滚动条停在指定位置
2个回答
推荐于2016-10-06 · 知道合伙人互联网行家
关注
展开全部
1、ScrollView 的滚动条默认置顶,若想设置它的位置,可以使用mScrollView.smoothScrollTo(x,y)方法,可达到效果。
2、crollTo方法可以调整view的显示位置。在需要的地方调用以下方法即可。
3、scroll表示外层的view,inner表示内层的view,其余内容都在inner里。
注意,方法中开一个新线程是必要的。否则在数据更新导致换行时getMeasuredHeight方法并不是最新的高度。
public static void scrollToBottom(final View scroll, final View inner) {
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
public void run() {
if (scroll == null || inner == null) {
return;
}
int offset = inner.getMeasuredHeight() - scroll.getHeight();
if (offset < 0) {
offset = 0;
}
scroll.scrollTo(0, offset);
}
});
}
2、crollTo方法可以调整view的显示位置。在需要的地方调用以下方法即可。
3、scroll表示外层的view,inner表示内层的view,其余内容都在inner里。
注意,方法中开一个新线程是必要的。否则在数据更新导致换行时getMeasuredHeight方法并不是最新的高度。
public static void scrollToBottom(final View scroll, final View inner) {
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
public void run() {
if (scroll == null || inner == null) {
return;
}
int offset = inner.getMeasuredHeight() - scroll.getHeight();
if (offset < 0) {
offset = 0;
}
scroll.scrollTo(0, offset);
}
});
}
2014-12-27
展开全部
public class LoadDocReader extends Activity
{
// private ScrollView mScrollView;
private TextView contentView;
private LinearLayout mLayout;
private String docInfo;
private String keyword;
private int keywordNum;
private ScrollView scrollview;
private final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置标题栏带有进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
String tag = "onCreate";
Log.d(tag, "initialize the new Activity");
setContentView(R.layout.reader);
mLayout = (LinearLayout) this.findViewById(R.id.textLayout);
/** the phone component initialization */
// mScrollView = (ScrollView) findViewById(R.id.scrollView);
contentView = (TextView) findViewById(R.id.textContent);
scrollview = (ScrollView)findViewById(R.id.scrollView);
// mLinearLayout = (LinearLayout) findViewById(R.id.textLayout);
//显示操作正在进行
loadData();
//取消显示
}
/**
* 载入书籍的数据
*/
private void loadData() {
Bundle bundle=getIntent().getExtras();
docInfo=bundle.getString("docStr");
String tempDocInfo = docInfo;
keyword = bundle.getString("keyword");
keywordNum = bundle.getInt("keywordNum");
int keywordIndex = tempDocInfo.indexOf(keyword);
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.FILL_PARENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
SpannableStringBuilder style=new SpannableStringBuilder(docInfo);
while(keywordIndex!=-1)
{
style.setSpan(new BackgroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int tempkeywordTempIndex = keywordIndex+keyword.length();
//style.setSpan(new ForegroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tempDocInfo = docInfo.substring(tempkeywordTempIndex,docInfo.length());
keywordIndex = tempDocInfo.indexOf(keyword);
if(keywordIndex!=-1)
{
keywordIndex = keywordIndex+tempkeywordTempIndex;
}
}
contentView.setText(style);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(contentView, params);
Toast.makeText(LoadDocReader.this,
String.valueOf(mLayout.getHeight()),
Toast.LENGTH_SHORT).show();
// scrollview.setScrollContainer(true);
// scrollview.setFocusable(true);
// mLayout.addView(contentView);
// scrollview.addView(mLayout);
mHandler.post(mScrollView);
// int tLine = contentView.getLineCount();
// int tBaseLine = contentView.getBaseline();
// int tBottom = contentView.getBottom();
// Toast.makeText(LoadDocReader.this,
// String.valueOf(tLine)+"*"+String.valueOf(tBaseLine)+"*"+String.valueOf(tBottom),
// Toast.LENGTH_SHORT).show();
// contentView.bringPointIntoView(30);
// mLayout.addView(contentView, params);
// mHandler.post(mScrollView);
}
private Runnable mScrollView = new Runnable() {
@Override
public void run() {
// int off = mLayout.getBottom()- scrollview.getHeight();
// Toast.makeText(LoadDocReader.this, String.valueOf(mLayout.getBottom())+"-----------"+String.valueOf(mLayout.getMeasuredHeight()),Toast.LENGTH_SHORT).show();
//// if (off > 0) {
scrollview.scrollTo(0, 30);//改变滚动条的位置
// }
}
};
}
{
// private ScrollView mScrollView;
private TextView contentView;
private LinearLayout mLayout;
private String docInfo;
private String keyword;
private int keywordNum;
private ScrollView scrollview;
private final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置标题栏带有进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
String tag = "onCreate";
Log.d(tag, "initialize the new Activity");
setContentView(R.layout.reader);
mLayout = (LinearLayout) this.findViewById(R.id.textLayout);
/** the phone component initialization */
// mScrollView = (ScrollView) findViewById(R.id.scrollView);
contentView = (TextView) findViewById(R.id.textContent);
scrollview = (ScrollView)findViewById(R.id.scrollView);
// mLinearLayout = (LinearLayout) findViewById(R.id.textLayout);
//显示操作正在进行
loadData();
//取消显示
}
/**
* 载入书籍的数据
*/
private void loadData() {
Bundle bundle=getIntent().getExtras();
docInfo=bundle.getString("docStr");
String tempDocInfo = docInfo;
keyword = bundle.getString("keyword");
keywordNum = bundle.getInt("keywordNum");
int keywordIndex = tempDocInfo.indexOf(keyword);
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.FILL_PARENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
SpannableStringBuilder style=new SpannableStringBuilder(docInfo);
while(keywordIndex!=-1)
{
style.setSpan(new BackgroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int tempkeywordTempIndex = keywordIndex+keyword.length();
//style.setSpan(new ForegroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tempDocInfo = docInfo.substring(tempkeywordTempIndex,docInfo.length());
keywordIndex = tempDocInfo.indexOf(keyword);
if(keywordIndex!=-1)
{
keywordIndex = keywordIndex+tempkeywordTempIndex;
}
}
contentView.setText(style);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(contentView, params);
Toast.makeText(LoadDocReader.this,
String.valueOf(mLayout.getHeight()),
Toast.LENGTH_SHORT).show();
// scrollview.setScrollContainer(true);
// scrollview.setFocusable(true);
// mLayout.addView(contentView);
// scrollview.addView(mLayout);
mHandler.post(mScrollView);
// int tLine = contentView.getLineCount();
// int tBaseLine = contentView.getBaseline();
// int tBottom = contentView.getBottom();
// Toast.makeText(LoadDocReader.this,
// String.valueOf(tLine)+"*"+String.valueOf(tBaseLine)+"*"+String.valueOf(tBottom),
// Toast.LENGTH_SHORT).show();
// contentView.bringPointIntoView(30);
// mLayout.addView(contentView, params);
// mHandler.post(mScrollView);
}
private Runnable mScrollView = new Runnable() {
@Override
public void run() {
// int off = mLayout.getBottom()- scrollview.getHeight();
// Toast.makeText(LoadDocReader.this, String.valueOf(mLayout.getBottom())+"-----------"+String.valueOf(mLayout.getMeasuredHeight()),Toast.LENGTH_SHORT).show();
//// if (off > 0) {
scrollview.scrollTo(0, 30);//改变滚动条的位置
// }
}
};
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询