相关疑难解决方法(0)

如何让OnClickListener使用RelativeLayout处理自定义视图?

你好,

我已经构建了一个自定义视图,RelativeLayout使用它作为自定义图像按钮.到目前为止,选择它有效(PIC2),甚至当我点击它(使用GoogleTV Remote)时,视图成功地将其状态更改为PIC3(由于android:duplicateParentState="true")

但不幸的是,onClickListener它不会触发(如果我点击带遥控器的视图"OK"按钮或者我使用触摸板也没关系..)

我真的需要像普通按钮一样的行为.怎么做到这一点?我已经花了几个小时的搜索谷歌和StackOverflow的...(顺便说一句,当设置android:clickable="false"RelativeLayout,在OnClickListener工作,但只有当我使用鼠标指针(触摸板),之后焦点将丢失,状态(如图3)没有显示)

图片

PIC1

自定义视图正常

PIC2

自定义视图聚焦

PIC3

单击自定义视图

rounded_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="false">

<TextView
    android:id="@+id/caption"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="30dp"
    android:background="@drawable/btn_rounded_corners"
    android:paddingLeft="25dp"
    android:textSize="15sp" 
    android:duplicateParentState="true"/>

<ImageView
    android:id="@+id/icon"
    style="@style/Menu_Button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="-50dp"
    android:layout_toLeftOf="@id/caption"
    android:background="@drawable/btn_main_menu_back_shape"
    tools:ignore="ContentDescription"
    android:duplicateParentState="true" />
Run Code Online (Sandbox Code Playgroud)

RoundedButton.java

public class RoundedButton extends RelativeLayout {
private String label;
private int icon;

/**
 * @param context
 */
public RoundedButton(Context context)
{
    super(context);
    initAttributes(context, …
Run Code Online (Sandbox Code Playgroud)

android google-tv

15
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

google-tv ×1