Android `“FrameLayout”中的RelativeLayout“set”正在捕获整个“FrameLayout”中的触摸事件`
编辑:我刚刚意识到,如果我想添加按钮或下面的任何内容,我希望能够在它们上面拖动。因此,我的解决方案可能是更改卡的Z索引,以便它们可以出现在Android `“FrameLayout”中的RelativeLayout“set”正在捕获整个“FrameLayout”中的触摸事件`,android,android-layout,touch,Android,Android Layout,Touch,编辑:我刚刚意识到,如果我想添加按钮或下面的任何内容,我希望能够在它们上面拖动。因此,我的解决方案可能是更改卡的Z索引,以便它们可以出现在框架布局之外 我有一个框架布局: <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/stack" /> 我将我的FrameLayout设置为match\u p
框架布局之外
我有一个框架布局
:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/stack"
/>
我将我的FrameLayout
设置为match\u parent
的唯一原因是,用户可以将相对布局拖动到屏幕上的任何位置,如果FrameLayout
的宽度和高度是wrap\u content
,则拖动时会裁剪RelativeLayout
。但是,将其保留为match\u parent
触摸FrameLayout
中的任意位置时,将触发LinearLayout
的触摸事件
在上下文中,我试图显示一堆大小相同的卡片,让用户一张一张地从屏幕上拖出最上面的一张。我最终直接添加到RelativeLayout
。不幸的是,在手动创建类时,一些重力设置丢失了,所以我做了如下操作:
int size = ScreenMetricUtil.convertPixelToDp(getActivity().getBaseContext(), 300);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
card.setLayoutParams(params);
container.addView(card); // add to LinearLayout
和我的助手方法():
int size = ScreenMetricUtil.convertPixelToDp(getActivity().getBaseContext(), 300);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
card.setLayoutParams(params);
container.addView(card); // add to LinearLayout
public static int convertPixelToDp(Context context, int pixels) {
float density = context.getResources().getDisplayMetrics().density;
return (int) (pixels * density + 0.5f);
}