保持按下时按钮背景不会改变

时间:2015-12-31 02:15:55

标签: android android-layout button

我在drawable中有以下xml。

<?xml version="1.0" encoding="utf-8"?>
<!--drawable/btn_send_comment.xml-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <layer-list>
            <item android:bottom="0dp" android:left="2dp" android:right="0dp" android:top="2dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/fab_color_shadow"/>
                    <corners android:radius="2dp"/>
                </shape>
            </item>

            <item android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/btn_send_normal"/>
                    <corners android:radius="2dp"/>
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:state_pressed="true">
        <shape android:bottom="0dp" android:left="2dp" android:right="0dp" android:shape="rectangle" android:top="2dp">
            <solid android:color="@color/btn_default_light_normal"/>
            <corners android:radius="2dp"/>
        </shape>
    </item>

</selector>

我有以下代码来更改按下按钮的状态:

package com.example.android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ViewAnimator;

import com.example.android.R;


public class SearchPeopleButton extends ViewAnimator implements View.OnClickListener {
    public static final int STATE_SELECTED = 0;
    public static final int STATE_NOT_SELECTED = 1;

Context ctx;
private static final long RESET_STATE_DELAY_MILLIS = 2000;

private int currentState;

private OnSendClickListener onSendClickListener;

private Runnable revertStateRunnable = new Runnable() {
    @Override
    public void run() {
        setCurrentState(STATE_SELECTED);
    }
};

public SearchPeopleButton(Context context) {
    super(context);
    init();
}

public SearchPeopleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    LayoutInflater.from(getContext()).inflate(R.layout.view_search_button, this, true);
    ctx = getContext();
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    currentState = STATE_SELECTED;
    super.setOnClickListener(this);
}

@Override
protected void onDetachedFromWindow() {
    removeCallbacks(revertStateRunnable);
    super.onDetachedFromWindow();
}

public void setCurrentState(int state) {

    currentState = state;
    if (state == STATE_NOT_SELECTED) {

    } else if (state == STATE_SELECTED) {
        setPressed(true);
    }
    showNext();
}

@Override
public void onClick(View v) {
    if (onSendClickListener != null) {
        onSendClickListener.onSendClickListener(this);
    }
}

public void setOnSendClickListener(OnSendClickListener onSendClickListener) {
    this.onSendClickListener = onSendClickListener;
}

@Override
public void setOnClickListener(OnClickListener l) {
    //Do nothing, you have you own onClickListener implementation (OnSendClickListener)
}

public interface OnSendClickListener {
    public void onSendClickListener(View v);
}

}

以下是我视图中的按钮用法:

        <com.example.android.view.SearchPeopleButton
        android:id="@+id/btnPeople"
        android:layout_width="72dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="8dp"
        android:background="@drawable/btn_send_comment"
        android:elevation="4dp"
        android:orientation="vertical"/>

这是我的view_search_button.xml

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

    <ImageView
        android:id="@+id/ivFeedAvatar"
        android:src="@drawable/ic_hashtag_grey"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</merge>

我可以看到我正在使用onClickEvents但仍然没有显示按下状态的颜色。

2 个答案:

答案 0 :(得分:1)

您可以使用ImageButton控件显示图像并支持按下状态。 http://developer.android.com/reference/android/widget/ImageButton.html

将按钮使用情况更改为:

<ImageButton
    android:id="@+id/btnPeople"
    android:layout_width="72dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="8dp"
    android:background="@drawable/btn_send_comment"
    android:elevation="4dp"
    android:orientation="vertical"/>

您可能还需要更改背景xml drawable,以在压缩和未压缩状态下包含正确的图像,具体取决于您要完成的任务。

答案 1 :(得分:0)

您好了解以下链接

步骤1.挑选主题的颜色和名称,然后根据您的要求应用并选择按钮等。复制并粘贴下载的代码并应用它将起作用的主题。

http://android-holo-colors.com/