这是我的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>
当我删除此滚动条然后 imageview 移动
MainActivity.Java时,上面是我的带有滚动条的 Xml 布局
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 移动得很好