Android - ImageButton点击

时间:2016-07-11 15:15:52

标签: android android-imagebutton

我试图让我的图片按钮看起来不错。我尝试过几种不同的方法,但看起来并不合适。它是一个圆形图像,我想让它看起来像是能够被按下。这是我到目前为止所得到的。 android:textAppearance ="?android:attr / textAppearanceLarge"通过显示方框使其看起来像按下按钮。如何开展这项工作?

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:layout_centerHorizontal="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/connectButton"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/myImage"
            android:layout_gravity="center"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Press To Connect To Device"
            android:id="@+id/connectMessage"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="10dp" />
    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone"
        android:indeterminateDrawable="@drawable/progress" >
    </ProgressBar>

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

您可以使用.xml文件作为按钮的drawable。只需选择按钮的背景为XML:

android:background="@drawable/selector1"

然后将可绘制文件夹中的selector1.xml定义为:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@color/cyan"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/magenta"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->
</selector>

您还可以选择不同的图像作为按钮背景。您也可以同时使用它:Button&amp; ImageButton

希望它有所帮助! :)

答案 1 :(得分:1)

为什么不在带有OnClickListener的java代码中执行此操作,而不是在XML中执行此操作?

你可以有两个图像,&#34;常规按钮&#34;和一个&#34;按下按钮&#34;图片。

然后点击,您可以将图像更改为&#34;按下按钮&#34;图像指定的时间(1 - 3秒),然后将其更改回&#34;常规按钮&#34;如果适用

答案 2 :(得分:0)

这绝不是一种最佳方式,但我找到的一种不错的小方法看起来就像我想要的那样。我为按钮按下了默认的两个xml(代码重复!)

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="200dp" />
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="5dip"
        android:color="#000000" />

    <gradient
        android:startColor="#000000"
        android:centerColor="#FFFFFF"
        android:endColor="#000000"
        android:angle="90">
    </gradient>
</shape>

round_button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="200dp" />
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="8dip"
        android:color="#000000" />

    <gradient
        android:startColor="#FFFFFF"
        android:centerColor="#000000"
        android:endColor="#FFFFFF"
        android:angle="90">
    </gradient>
</shape>

现在,当您创建按钮时添加此项。虽然不是最佳的,但是该方法允许不同类型的用户点击动作和样式。

connectButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        connectButton.setBackgroundResource(R.drawable.round_button_press);
                        return true;
                    }else if(event.getAction() == MotionEvent.ACTION_UP){
                        connectButton.setBackgroundResource(R.drawable.round_button);
                        connect();
                        return true;
                    }else{
                        Log.e(TAG, event.getAction() + "");
                        return true;
                    }

                }
            });
相关问题