首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android拖拽图片

Android拖拽图片
EN

Stack Overflow用户
提问于 2013-02-19 23:48:19
回答 1查看 395关注 0票数 0

我的布局中有一个图像视图和一个文本视图,我想用触摸来移动这个图像视图。它可以工作,但问题是文本视图也移动了,你能告诉我如何修复它吗?

这是我的代码

代码语言:javascript
复制
private ImageView img;
private int status=0;
TextView tv;
RelativeLayout.LayoutParams params;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     img=(ImageView)findViewById(R.id.imageView1);
     img.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub

            status=1;

        return false;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return false;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

    if( status==1)
    {
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lp.topMargin=(int)event.getRawY();
        lp.leftMargin=(int)event.getRawX();
        img.setLayoutParams(lp);
    }
    if(event.getAction()==MotionEvent.ACTION_UP){
    status=0;
    }
return super.onTouchEvent(event);

}}

这是我的XML

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="84dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="119dp"
    android:text="TextView" />

EN

回答 1

Stack Overflow用户

发布于 2013-02-19 23:57:35

案例结束了,天哪,我忘了,因为我使用了相对布局,文本视图左对齐到了imageview1谢谢帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14961523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档