-1

在我的按钮上的 xml 中,我想声明图像,当我向下拖动图像时,按钮单击不起作用,它在后台,它确实出现在前台。

怎么可能使用按钮?当我点击按钮时,我想显示吐司信息。

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>
4

1 回答 1

0

如果您将图像放在按钮上,按钮将失去控制,您可以使用背景或可绘制的左或右顶部底部为按钮提供可绘制(图像)......或者即使在图像视图上也可以在点击侦听器上实现......更新。 .

<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" />

在我给drawableleft的代码中,同样你可以给右上角,甚至填充也是可能的,并应用你的触摸逻辑

于 2013-11-15T10:26:20.347 回答