gpt4 book ai didi

java - 我想滚动相对布局也想拖动相对布局中的 ImageView 但按钮不会在 ScrollView 中拖动

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

这是我的xml布局在这里

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/vg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<ImageView
android:id="@+id/img1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:src="@drawable/button" />

</RelativeLayout>

</ScrollView>

上面是我的带滚动条的Xml布局当我删除此滚动条然后 imageview 移动
MainActivity.Java

package com.example.wordcombat1;


import android.net.wifi.WifiConfiguration.Status;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class MainActivity extends Activity {

private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
int status = 0;
int windowwidth,windowheight;
ImageView tv1;
RelativeLayout.LayoutParams layoutParams1;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup vg = (ViewGroup) findViewById(R.id.vg);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.img1);
tv1.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
}

}

任何人都可以帮助我如何移动这两个东西。相对布局也可以滚动, ImageView 也可以正确拖动。没有滚动条的imageview移动的很好

最佳答案

使用 GridView 而不是 ScrollView 我认为您的问题已解决

关于java - 我想滚动相对布局也想拖动相对布局中的 ImageView 但按钮不会在 ScrollView 中拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395620/

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