问题更改按钮大小时pressed Android中 [英] Problem changing button size when pressed in Android

查看:190
本文介绍了问题更改按钮大小时pressed Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变我的按钮的大小,当我preSS,但没有什么似乎工作。

I have tried changing the size of my button when I press it but nothing seem to work.

按钮在main.xml中创建的:

The button is created in main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:layout_alignParentLeft="true" 
android:layout_height="90px" 
android:id="@+id/button1" 
android:layout_marginTop="120px"
android:layout_width="90px"         
android:textSize="45px" 
android:textStyle="bold"
android:background="@drawable/bnumber" 
android:text="1">
</Button>

而$ C $下按钮的逻辑是:

And the code for the button logic is:

button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                sendMessage("key1\n");
                button1.setWidth(140);
                button1.setHeight(140);
            }
            if(event.getAction() == MotionEvent.ACTION_UP) {
                button1.setWidth(90);
                button1.setHeight(90);
            }
            return false;
        }
    });

从谷歌上搜索我敢肯定这应该工作,但由于某些原因,它没有。

From googling I am quite sure this should work, but for some reason it doesn't.

我的设备采用了Android 1.6,可这是什么问题?

My device uses Android 1.6, could this be the problem?

推荐答案

您可以使用下面的code设置按钮的宽度和高度设置按钮的宽度和高度

You can set button width and height using the following code to set button width and height

button1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        RelativeLayout.LayoutParams lp = (LayoutParams) b.getLayoutParams();
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            sendMessage("key1\n");
            lp.width=140;
            lp.height=140;
            button1.setLayoutParams(lp);
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            lp.width=90;
            lp.height=90;
            button1.setLayoutParams(lp);
        }
        return false;
    }
});

谢谢 迪帕克

Thanks Deepak

这篇关于问题更改按钮大小时pressed Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆