Android 在滚动视图中的相对布局之间动态滚动
我有一个滚动视图,其中有几个相对的布局。一个相对布局有两个动态添加的按钮。如下所示Android 在滚动视图中的相对布局之间动态滚动,android,android-layout,android-scrollview,Android,Android Layout,Android Scrollview,我有一个滚动视图,其中有几个相对的布局。一个相对布局有两个动态添加的按钮。如下所示 _____________________ | ________________ | | |Relative Layout| | | | |Buttons| | | | |_______________| | | | ________________ | | |Relative Layout|
_____________________
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| |Relative Layout| |
| | |Buttons| | |
| |
|_____________________|
滚动视图
_____________________
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| ________________ |
| |Relative Layout| |
| | |Buttons| | |
| |_______________|
| |
| |Relative Layout| |
| | |Buttons| | |
| |
|_____________________|
是否可以通过单击按钮动态滚动到特定的相对布局。
我已经在下面试过了,它不起作用
if (count == 1) {
final int k = id;
mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated
// method stub
mScrollView.post(new Runnable() {
@Override
public void run() {
Button btn = (Button) findViewById(k);
mScrollView.smoothScrollTo(0,btn.getTop());
}
});
滚动至也不工作
if (count == 1) {
final int k = id;
mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated
// method stub
mScrollView.post(new Runnable() {
@Override
public void run() {
Button btn = (Button) findViewById(k);
mScrollView.smoothScrollTo(0,btn.getTop());
}
});
编辑:
最初,我在相对布局中只有一组按钮。然后,
smoothScrollTo
按预期工作。后来我把结构改成了上面的样式 @Daniel Bo的暗示是正确的
view.getTop()
将给出相对于其直接父对象的位置。
下面是实现
ScrollView mScrollView = (ScrollView) findViewById(R.id.scroll_view);
mScrollView.post(new Runnable() {
public void run() {
Button btn = (Button) findViewById(k);
ViewGroup vg =(ViewGroup)btn.getParent();
mScrollView.smoothScrollTo(0,vg.getTop());
}});
}
欢迎使用stackoverflow,下次请添加格式良好的代码。您有用于此布局的XML布局吗?你也可以发布这篇文章吗?我正在用xml创建下面的滚动视图,在上面我通过编辑你的文章而不是添加注释来添加相对布局动态添加代码。除此之外,btn.getTop(),这不是给您留下了相对于其父对象的相对位置吗?所以我假设您想要它的父对象的位置,sourrounding Relativelayout