gpt4 book ai didi

android - RelativeLayout - 拖动自定义 View

转载 作者:行者123 更新时间:2023-11-30 03:52:48 24 4
gpt4 key购买 nike

我有一个带有 onTouch() 监听器的自定义 View 。此 View 被添加到 RelativeLayout 中,因此:

CustomView mv = new CustomView(mActivity);
RelativeLayout rl = ((RelativeLayout) mActivity.findViewById(R.id.main_layout));
rl.addView(mv);

我希望在 API8 下用手指拖动 View ,最好是通过驻留在 View 自己的 onTouch() 监听器中的代码。

可能是setPadding()或RelativeLayout.LayoutParams()边缘的几行代码,但经过一个小时的反复试验,我找不到它。

有什么线索吗?非常感谢。

PS:onTouch 正常工作,我只是不知道我可以添加哪些代码行来移动 View 。

最佳答案

我建议使用 FrameLayout,然后像这样更改 CustomView 的边距:

LayoutParams params = (FrameLayout.LayoutParams) mv.getLayoutParams();
params.leftMargin = x;
params.topMargin = y;
mv.setLayoutParams(params);

希望这对您有所帮助。

关于android - RelativeLayout - 拖动自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13895422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com