2013-11-15 32 views
-1

在我的xml按钮上,我想声明图像,当我拖动图像时,按钮单击不起作用,它在背景中,它确实来了forground。当我拖下图像时,按钮不起作用

怎么可能使用按钮?当我点击按钮时,我想要显示烤面包片。

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    ImageView img = null; 
    AbsoluteLayout aLayout; 
    int status = 0; 
    int Measuredwidth = 0, MeasuredHeight = 0; 
    float x = 0; 
    boolean first = true; 
    Bitmap icon; 
    Button b; 

    @SuppressLint("NewApi") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     aLayout = (AbsoluteLayout) findViewById(R.id.absLayout); 

     img = (ImageView) findViewById(R.id.imageView); 
     icon = BitmapFactory.decodeResource(getResources(), R.drawable.gg); 

     b = (Button) findViewById(R.id.btnFavorites); 
     Point size = new Point(); 
     WindowManager w = getWindowManager(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
      w.getDefaultDisplay().getSize(size); 
      MeasuredHeight = size.y; 
      Measuredwidth = size.x; 
     } else { 
      Display d = w.getDefaultDisplay(); 
      Measuredwidth = d.getWidth(); 
      MeasuredHeight = d.getHeight(); 
     } 

     b.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       b.setVisibility(View.GONE); 
       Toast.makeText(MainActivity.this, "activity closed", 
         Toast.LENGTH_LONG).show(); 

      } 
     }); 

     aLayout.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if (event.getAction() == MotionEvent.ACTION_UP) { 
        status = 0; 

        // img.setBackgroundColor(Color.TRANSPARENT); 

       } else if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        if (first) { 
         x = event.getX(); 
         first = false; 
        } 
       } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 
        // 
       } 
       if ((event.getX() < (Measuredwidth - icon.getWidth()/2) && event 
         .getX() > (icon.getWidth()/2)) 
         && (event.getY() < MeasuredHeight 
           - (icon.getHeight() + 60) && event.getY() > icon 
           .getHeight()/2)) { 
        @SuppressWarnings("deprecation") 
        // (int) event.getX()-img.getWidth()/2 
        LayoutParams lp = new AbsoluteLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT, (int) 0, (int) event 
            .getY() - img.getHeight()/2); 
        img.setLayoutParams(lp); 
       } 

       return true; 
      } 
     }); 

    } 

} 

布局

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <Button 
    android:id="@+id/btnFavorites" 
    android:layout_width="match_parent" 
    android:layout_height="45dip" 
    android:drawablePadding="10dp" 
    android:paddingLeft="10dp" 
    android:layout_marginTop="1dip" 
    android:gravity="left|center_vertical" 
    android:background="#242D41" 
    android:drawableLeft="@drawable/ic_launcher" 
    android:includeFontPadding="false" 

    android:textColor="#ffffff" 
    android:textSize="15dip" 
    android:textStyle="bold" /> 

    <AbsoluteLayout 
     android:id="@+id/absLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="1dip" > 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/gg" 
      android:scaleType="matrix" > 
     </ImageView> 
    </AbsoluteLayout> 

</FrameLayout> 
+0

为什么不使用按钮中的android:background属性?那里有什么使用imageview的用法? – Avijit

+0

是的,我宣布android:背景属性,按钮宽松控制,使用imageview是iwnt拖放图像在屏幕上 – user3114805

回答

0

如果你把上的按钮图像,按钮就会失去控制的话,你可以使用背景或绘制的左侧或右侧顶部底部给绘制(图像)按钮......或者否则,你可以在我给drawableleft代码上点击听众甚至在图像视图.. 更新实现..

<Button 
    android:id="@+id/btnFavorites" 
    android:layout_width="match_parent" 
    android:layout_height="45dip" 
    android:drawablePadding="10dp" 
    android:paddingLeft="10dp" 
    android:layout_marginTop="1dip" 
    android:gravity="left|center_vertical" 
    android:background="#242D41" 
    android:drawableLeft="@drawable/favorite_slide" 
    android:includeFontPadding="false" 
    android:text="@string/titleFavourites" 
    android:textColor="#ffffff" 
    android:textSize="15dip" 
    android:textStyle="bold" /> 

,同样哟可以给正确的左上角,甚至填充也可以,并应用您的逻辑触摸

+0

嗨,先生,我宣布“android:background = @ grawable/ic_lanuncher”,但我不工作,你可以给我发code.please。 – user3114805

+0

我刚添加了一些示例代码的按钮检查.. – pavanmvn

+0

没有使用这样的声明also.button是不是来forground.can你告诉我怎么可能please.that是我的实际code.how可以得到按钮forground – user3114805