2012-07-24 72 views
0

我尽力谁能帮助阻力和Android的拖放的TextView上的ImageView android系统

我的XML与框架布局

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

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:scaleType="center" 
    android:src="@drawable/golden_gate" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dip" 
    android:layout_gravity="center_horizontal|bottom" 

    android:padding="12dip" 

    android:background="#AA000000" 
    android:textColor="#ffffffff" 

    android:text="Golden Gate" /> 

</FrameLayout> 

我试图拖放在imageview的下降TextView的文字下面是我用这里的线性布局中的文本是拖在某些部分不仅没有图像

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
public class DragNDropActivity extends Activity { 
     private View selected_item = null; 
     private int offset_x = 0; 
     private int offset_y = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ViewGroup vg = (ViewGroup)findViewById(R.id.vg); 
     vg.setOnTouchListener(new View.OnTouchListener() { 

         @Override 
         public boolean onTouch(View v, MotionEvent event) { 
           switch(event.getActionMasked()) 
           { 
             case MotionEvent.ACTION_MOVE: 
               int x = (int)event.getX() - offset_x; 
               int y = (int)event.getY() - offset_y; 
         int w = getWindowManager().getDefaultDisplay().getWidth() - 100; 
         int h = getWindowManager().getDefaultDisplay().getHeight() - 100; 
         if(x > w) 
          x = w; 
         if(y > h) 
          y = h; 
             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             new ViewGroup.MarginLayoutParams(
                 LinearLayout.LayoutParams.WRAP_CONTENT, 
                 LinearLayout.LayoutParams.WRAP_CONTENT)); 
             lp.setMargins(x, y, 0, 0); 
               selected_item.setLayoutParams(lp); 
               break; 
             default: 
               break; 
           } 
           return true; 
         } 
    }); 
     TextView img = (TextView)findViewById(R.id.img); 
     img.setOnTouchListener(new View.OnTouchListener() { 

         @Override 
         public boolean onTouch(View v, MotionEvent event) { 
           switch(event.getActionMasked()) 
           { 
             case MotionEvent.ACTION_DOWN: 
               offset_x = (int)event.getX(); 
               offset_y = (int)event.getY(); 
               selected_item = v; 
               break; 
             default: 
               break; 
           } 

           return false; 
         } 
       }); 
    } 
} 
+0

我用框架布局ñ也是线性布局,但我不能拖和图像 – Bhavika 2012-07-24 07:50:01

+0

式文本,请给我的想法有关其建立可以使用API​​级别?或者你的目标API级别我对API级别11或更高版本的一个演示。 – 2012-10-05 07:32:33

回答

0

尝试使用框架布局应该弄清楚你想要什么。

+0

我用但不工作的 – Bhavika 2012-07-24 07:35:42

+0

你可以请发表您的布局XML – 2012-07-24 07:36:14

+0

你的代码工作在我的日食,但现在我可绘 – 2012-07-24 07:43:16

0

使用RelativeLayout的这样。注意:你可以把它放在你的父母视图里面你的layout.xml里面的

<RelativeLayout android:layout_width="100dp" android:id="@+id/photoframe" android:layout_height="320dp" android:layout_weight="1.0"> 
     <ImageView android:layout_height="fill_parent" android:scaleType="fitXY" android:id="@+id/framephoto" android:src="@drawable/icon" android:layout_width="fill_parent"></ImageView> 
     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/textView1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="188dp"></TextView> 
    </RelativeLayout> 

。通过使用相对可以拖动你的TextView了你的形象。

0

请试试这个....

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">   
    <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher" android:scaleType="center" /> 
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#AA000000" android:textColor="#ffffffff" android:text="Golden Gate"/> 
</FrameLayout> 

我和eclipse.And检查的话让我知道是工作或没有。