Java 将TableLayout中的文本视图滚动到屏幕的可见区域
如果Java 将TableLayout中的文本视图滚动到屏幕的可见区域,java,android-widget,scrollview,Java,Android Widget,Scrollview,如果文本视图不是滚动视图的直接子对象,如何通过滚动滚动视图将文本视图放入屏幕的可见区域 我有一个LinearLayout视图,它的顶部有一个TextView,然后是一个ScrollView,下面是一个按钮: <LinearLayout android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_height="
文本视图
不是滚动视图
的直接子对象,如何通过滚动滚动视图
将文本视图
放入屏幕的可见区域
我有一个LinearLayout
视图,它的顶部有一个TextView
,然后是一个ScrollView
,下面是一个按钮
:
<LinearLayout
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"/>
<ScrollView
android:id="@+id/s"
android:layout_height="0dip"
android:layout_weight="1" >
<TableLayout
android:id="@+id/t"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/r" >
<TextView
android:id="@+id/a"/>
<TextView
android:id="@+id/b"/>
<TextView
android:id="@+id/c"/>
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_height="wrap_content"/>
</LinearLayout>
它生成了一个完全填充的屏幕,标签在上边框,按钮在下边框,中间有一张桌子;根据此表的行数,您可以滚动或不滚动它
表的内容是由Java代码生成的,我只是添加了一行作为插入内容的示例
我现在需要通过垂直滚动来确保某一行(不一定是最后一行)是可见的。我可以访问从s
到c
的每一个元素,但我无法找出使滚动视图s
滚动的代码
我在屏幕上尝试了requestchildrectangle
、scrollBy
和scrollTo
,但可能总是使用错误的参数
现在我不在乎TextView a
是垂直居中还是在底部或顶部边框,如果它是可见的,那就太好了。X-scroll最好保持在0(即最左边的位置)
在这种情况下,这一点很重要:只有当用户带着特殊意图进入屏幕,告诉屏幕滚动到TableRow
x(只是显示最新的更改)时,才会调用此函数
如果您需要更多信息,请询问
谢谢大家! 问题确实是我直接从
onCreate
的子调用调用了该函数。这意味着布局还没有完成,因此所有的滚动都是在元素有高度之前完成的,因此滚动了0像素
解决方案是使用View.post(Runnable r)
:
ScrollView s = (ScrollView) findViewById(R.id.s);
TextView a = (TextView) findViewById(R.id.a);
s.post(new Runnable() {
public void run() {
int[] location = new int[2];
a.getLocationInWindow(location);
int y = location[1];
s.getLocationInWindow(location);
s.scrollTo(0, y-location[1]); // or some other calculation
}
});