如何控制ScrollView的滚动条,让滚动条停在指定位置? 停到某个关键字出再的地方,或者停到某一行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那还是那个计算位置的问题,算出有多少行文本,然后找到关键词在第几行,然后根据屏幕能显示的最大行数通过scrollto的方法来手动定位滚动条的位置。 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 void openFile(File f) throws IOException{ String fileName = f.getName(); String prefix=fileName.substring(fileName.lastIndexOf(".")+1); if(prefix.equals("doc")) { FileInputStream in = new FileInputStream(f); WordExtractor extractor = new WordExtractor(in); String docStr = extractor.getText(); // String docSubStr = docStr.substring(0, docStr.length()); Intent it = new Intent(MyFile.this, LoadDocReader.class); Bundle bundle=new Bundle(); bundle.putString("docStr", docStr); bundle.putString("keyword", keyword); bundle.putInt("keywordNum", keywordNum); it.putExtras(bundle); startActivity(it); // finish(); } else if(prefix.equals("txt")) { InputStream in = new BufferedInputStream(new FileInputStream(f)); BufferedReader br = new BufferedReader(new InputStreamReader(in, "gb2312")); String txtStr = ""; String tempTxtStr = br.readLine(); while(tempTxtStr!=null) { txtStr = txtStr+tempTxtStr+"\n"; tempTxtStr = br.readLine(); } Intent it = new Intent(MyFile.this, LoadDocReader.class); Bundle bundle=new Bundle(); bundle.putString("keyword", keyword); bundle.putString("docStr", txtStr); bundle.putInt("keywordNum", keywordNum); it.putExtras(bundle); startActivity(it); // finish(); } else if(prefix.equals("pdf")) { ; }} 有这一句程序就出错了。LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); mLayout.addView(contentView, params);但是下面的数据也有问题: 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();tLine = 0;tBaseLine = -1;tBottom = 0;但是屏幕上显示的有内容。<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical"> <LinearLayout android:id="@+id/textLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textContent" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout></ScrollView> LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); mLayout.addView(contentView, params);add之前先remove 没看清loadData里addview肯定不行,你是在oncreate里做的,而且是在已经setContentView之后又addview,只能放在setContentView前add。 TextView 里面明明显示的有数据,为什么调用它的三个函数:getLinesCount==0;getBaseline==-1;getBottom==0? 我找到答案了,我们可以这样来做,scrollview.post(new Runnable() { @Override public void run() { scrollview.scrollTo(70, 0); } });这样就不管是在oncreate里面还是其它地方都有用了,至于为什么 ,我想你一看应该能明白吧 在View为measure之前获取它的宽度和高度,肯定都是0. 这样计算出来的也是错的。 在View未measure之前获取它的宽度和高度,获取到的值是0. 这样依赖于该结果的计算也是错的。 自定义的View怎么控制View的高度 小弟新手 向找点android开发项目视频 求教--使用HandlerThread构造Child Handler出现空指针问题 使用handler仍然出现UI假死,求解? 如何在一个普通的View中得到输入的中文 关于listview的问题 Linux Driver思考:通向皇宫的通道 android 解析json 怎么让app异常后自己重启 QVGA Slider和QVGA有什么不同? android4.0 支持泰国语吗? android布局中android:layout_gravity="right"的问题
算出有多少行文本,然后找到关键词在第几行,然后根据屏幕能显示的最大行数通过scrollto的方法来手动定位滚动条的位置。
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 void openFile(File f) throws IOException{
String fileName = f.getName();
String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
if(prefix.equals("doc"))
{
FileInputStream in = new FileInputStream(f);
WordExtractor extractor = new WordExtractor(in);
String docStr = extractor.getText();
// String docSubStr = docStr.substring(0, docStr.length());
Intent it = new Intent(MyFile.this, LoadDocReader.class);
Bundle bundle=new Bundle();
bundle.putString("docStr", docStr);
bundle.putString("keyword", keyword);
bundle.putInt("keywordNum", keywordNum);
it.putExtras(bundle);
startActivity(it);
// finish();
}
else if(prefix.equals("txt"))
{
InputStream in = new BufferedInputStream(new FileInputStream(f));
BufferedReader br = new BufferedReader(new InputStreamReader(in, "gb2312"));
String txtStr = "";
String tempTxtStr = br.readLine();
while(tempTxtStr!=null)
{
txtStr = txtStr+tempTxtStr+"\n";
tempTxtStr = br.readLine();
}
Intent it = new Intent(MyFile.this, LoadDocReader.class);
Bundle bundle=new Bundle();
bundle.putString("keyword", keyword);
bundle.putString("docStr", txtStr);
bundle.putInt("keywordNum", keywordNum);
it.putExtras(bundle);
startActivity(it);
// finish();
}
else if(prefix.equals("pdf"))
{
;
}
}
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(contentView, params);
但是下面的数据也有问题: 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();tLine = 0;
tBaseLine = -1;
tBottom = 0;
但是屏幕上显示的有内容。<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:scrollbars="vertical">
<LinearLayout android:id="@+id/textLayout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/textContent" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(contentView, params);add之前先remove
loadData里addview肯定不行,你是在oncreate里做的,而且是在已经setContentView之后又addview,只能放在setContentView前add。
我找到答案了,我们可以这样来做,
scrollview.post(new Runnable() {
@Override
public void run() {
scrollview.scrollTo(70, 0);
}
});这样就不管是在oncreate里面还是其它地方都有用了,至于为什么 ,我想你一看应该能明白吧