我实际上尝试了一些非常简单的事情,但是 android 和 java 让我的生活变得不那么容易。这个想法是滚动到表格布局中的指定子项。
我正在使用 Layoutinflater 将条目添加到表格布局中,如下所示:
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout3);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.element_news, null);
TextView firstname = (TextView) itemView.findViewById(R.id.tvname);
firstname.setText(FN + " " + iLN);
TextView date = (TextView) itemView.findViewById(R.id.tvdate);
date.setText(newPD);
TextView post = (TextView) itemView.findViewById(R.id.tvpost);
post.setText(c.getString(iP));
post.setFocusable(true);
tl.addView(itemView, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
我尝试将 getChildAt() 用于 tablelayout,我确实得到了 child ,但是当我使用 child 时,它返回所有“0”。
示例代码:
LinearLayout row = (LinearLayout) tl.getChildAt(1);
TextView tv = (TextView) row.getChildAt(1);
tv.requestFocus();
Log.w("news", Integer.toString(tv.getHeight()));
对于 TextView 高度,它返回“0”,即使它包含多行并且 requestfocus() 也不起作用。
那么我怎样才能滚动到表格布局中的 child ?
在此先感谢您的帮助。
最佳答案
该代码示例不起作用,因为您可能在 onCreate
方法中使用了它,而此时 UI 尚未绘制,因此 View 没有任何尺寸。如果是这种情况,您可以简单地在您的一个 View 上发布一个 Runnable
以延迟一点维度收集,直到 onCreate
方法之后,如下所示:
// post the Runnable
tl.post(new Runnable() {
@Override
public void run() {
LinearLayout row = (LinearLayout) tl.getChildAt(1);
// get the top position relative to the parent
int top = tr.getTop();
// scroll to that top position the wrapping ScrollView
sv.scrollTo(0, top);
}
});
您的代码有些奇怪(从我的角度来看),如果上面的代码不起作用,您应该发布有关您的布局文件和使用场景的更多详细信息。
关于android - tablelayout getchildat 滚动到那个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13492854/