您好我实现了在整个屏幕周围移动textview的代码,它可以在整个屏幕上正常工作 但是现在我想在有限的屏幕上实现它
假设有200dp高度和200宽度的相对布局,因为有一个textview和一个imageview
如下所示
--RelativeLayout
--ImageView
--TextView
但是textview不会正常移动它会快速消失,就像它移动更多
下面是我的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/txtHour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
和javacode位于
之下 public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtHour = (TextView) findViewById(R.id.txtHour);
txtHour.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
drag(event, v);
return true;
}
});
}
public void drag(MotionEvent event, View v)
{
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
params.topMargin = (int)event.getRawY() - (v.getHeight());
params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_UP:
{
params.topMargin = (int)event.getRawY() - (v.getHeight());
params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_DOWN:
{
v.setLayoutParams(params);
break;
}
}
}
}
当我的手指在那个区域移动时,任何身体能告诉我如何在有限的区域内移动它吗?