Java 如何通过触摸将imageview置于其他imageview之上(android)
是左边的圆圈 所以我有imageview=(shaped drawble src),当我触摸它时,将drawble res更改为other 所以颜色改变了。但我也想把它放在其他图像视图之上。我怎么能做到呢?我尝试了bringToFront,但不起作用,还有其他方法吗 当我按下一个图像时,我希望所有的图像都是相同的,因为它会在其他图像之前显示Java 如何通过触摸将imageview置于其他imageview之上(android),java,android,xml,android-layout,Java,Android,Xml,Android Layout,是左边的圆圈 所以我有imageview=(shaped drawble src),当我触摸它时,将drawble res更改为other 所以颜色改变了。但我也想把它放在其他图像视图之上。我怎么能做到呢?我尝试了bringToFront,但不起作用,还有其他方法吗 当我按下一个图像时,我希望所有的图像都是相同的,因为它会在其他图像之前显示 final ImageView imageView = (ImageView) findViewById(R.id.cir);
final ImageView imageView = (ImageView) findViewById(R.id.cir);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.cir_press);
break;
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.cir);
break;
}
}
return false;
}
});
或
或
使用RelativeLayout作为图像视图的父视图,在每个图像视图上单击调用视图。bringToFront()
使用RelativeLayout作为图像视图的父视图,并在每个图像视图上单击调用视图。bringToFront()
我可能会迟到,但我注意到您在布局中给出了高程imageView.bringToFront()
如果你给你的视图提供不同的高程,那么它将不起作用,而且你的@+id/layoutTop
设置为match\u parent
,这可能会导致imageView.setOnTouchListener
停止工作,所以我已经这样调整了你的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:gravity="bottom|center">
<ImageView
android:id="@+id/cir"
android:layout_width="184dp"
android:layout_height="313dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_marginBottom="43dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir2"
android:layout_width="150dp"
android:layout_height="186dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="211dp"
android:layout_marginLeft="211dp"
android:layout_marginTop="38dp"
android:layout_marginBottom="132dp"
android:gravity="center"
android:padding="5dp"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir3"
android:layout_width="205dp"
android:layout_height="140dp"
android:layout_alignBottom="@+id/cir2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="-127dp"
android:padding="5dp"
android:src="@drawable/cir"/>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="wrap_content"
android:layout_height="231dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:gravity="bottom">
<ImageView
android:id="@+id/cir4"
android:layout_width="wrap_content"
android:layout_height="212dp"
android:layout_alignWithParentIfMissing="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:gravity="bottom|center"
android:src="@drawable/cir"
/>
</RelativeLayout>
</RelativeLayout>
我可能会迟到回答,但我注意到您在布局中给出了高程imageView.bringToFront()
如果你给你的视图提供不同的高程,那么它将不起作用,而且你的@+id/layoutTop
设置为match\u parent
,这可能会导致imageView.setOnTouchListener
停止工作,所以我已经这样调整了你的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:gravity="bottom|center">
<ImageView
android:id="@+id/cir"
android:layout_width="184dp"
android:layout_height="313dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_marginBottom="43dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir2"
android:layout_width="150dp"
android:layout_height="186dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="211dp"
android:layout_marginLeft="211dp"
android:layout_marginTop="38dp"
android:layout_marginBottom="132dp"
android:gravity="center"
android:padding="5dp"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir3"
android:layout_width="205dp"
android:layout_height="140dp"
android:layout_alignBottom="@+id/cir2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="-127dp"
android:padding="5dp"
android:src="@drawable/cir"/>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="wrap_content"
android:layout_height="231dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:gravity="bottom">
<ImageView
android:id="@+id/cir4"
android:layout_width="wrap_content"
android:layout_height="212dp"
android:layout_alignWithParentIfMissing="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:gravity="bottom|center"
android:src="@drawable/cir"
/>
</RelativeLayout>
</RelativeLayout>
你能提供xml布局吗?@nik检查xml代码这里的问题之一是布局不一致,嵌套的RL有匹配的父对象,所以当你触摸某个地方时,你也可以点击它。我建议您使用ConstraitLayout作为根目录,并且只使用1级嵌套。然后尝试替换当前设置click listener或touch(如果坚持)的启发式设置,先设置parent.removeView(视图),然后设置parent.addView(视图)您可以提供xml布局吗?@nik检查xml代码这里的问题之一是布局不一致,嵌套的RL具有匹配的父级,所以,当你触摸某个地方时,你也可以轻敲它。我建议您使用ConstraitLayout作为根目录,并且只使用1级嵌套。然后尝试替换当前设置单击侦听器或触摸的启发式设置(如果坚持的话),设置为parent.removeView(视图),然后设置为parent.addView(视图),但这将使所有这些设置都位于前面,我只需要在触摸或单击位于其他设置之前时设置一个。作者将视图
称为触摸视图,与getView方法或父视图不同,请尝试一下,它听起来像是imageView1.setOnClickListener(new view.OnClickListener(){@Override public void onClick(view视图){view.bringToFront()}});但是这将使所有的视图都位于前面,当我触摸或单击“位于其他视图的前面”时,我只需要一个。作者说的是作为触摸视图的view
,而不是在getView方法或父视图中,试试看,它听起来很有逻辑它看起来像这个imageView1.setOnClickListener(new view.OnClickListener(){@Override public void onClick(视图){View.bringToFront()}});
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:src="@drawable/your image" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:gravity="bottom|center">
<ImageView
android:id="@+id/cir"
android:layout_width="184dp"
android:layout_height="313dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_marginBottom="43dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir2"
android:layout_width="150dp"
android:layout_height="186dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="211dp"
android:layout_marginLeft="211dp"
android:layout_marginTop="38dp"
android:layout_marginBottom="132dp"
android:gravity="center"
android:padding="5dp"
android:src="@drawable/cir"
/>
<ImageView
android:id="@+id/cir3"
android:layout_width="205dp"
android:layout_height="140dp"
android:layout_alignBottom="@+id/cir2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="-127dp"
android:padding="5dp"
android:src="@drawable/cir"/>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="wrap_content"
android:layout_height="231dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:gravity="bottom">
<ImageView
android:id="@+id/cir4"
android:layout_width="wrap_content"
android:layout_height="212dp"
android:layout_alignWithParentIfMissing="true"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:gravity="bottom|center"
android:src="@drawable/cir"
/>
</RelativeLayout>
</RelativeLayout>
final ImageView imageView = (ImageView) findViewById(R.id.cir);
final ImageView topLayout = (ImageView) findViewById(R.id.layoutTop);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("IMAGE", "motion event: " + event.toString());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
imageView.setImageResource(R.drawable.cir_press);
imageView.bringToFront();
break;
}
case MotionEvent.ACTION_UP: {
imageView.setImageResource(R.drawable.cir);
topLayout.bringToFront();
break;
}
}
return false;
}
});