如何控制ScrollView的滚动条,让滚动条停在指定位置

 我来答
志当存高远389
2015-05-30 · 知道合伙人互联网行家
志当存高远389
知道合伙人互联网行家
采纳数:11237 获赞数:20406
08年毕业,一直从事计算机行业,从基层做起。有较强的实践操作能力。

向TA提问 私信TA
展开全部
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);//改变滚动条的位置
// }
}

};

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式