gpt4 book ai didi

android - LinearLayout 作为自定义按钮,OnClickListener 从不调用

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:25 25 4
gpt4 key购买 nike

我一直在使用带有图标 (drawableTop) 和文本的通用 Android 按钮。如果你想要一个非标准尺寸的按钮,它的效果真的很差,所以我决定用具有以下布局的 LinearLayout 制作一个自定义按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ButtonHoloDark"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:orientation="vertical" >

<ImageView
android:id="@+id/buttonIcon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true" />

<TextView
android:id="@+id/buttonText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:gravity="center"
android:textColor="@color/white" />

</LinearLayout>

布局由自定义类使用:

public class CustomIconButton extends LinearLayout {
public CustomIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
setAttributes(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_icon_button, this, true);
}

...

但是当我在其父布局中的按钮上设置 OnClickListener 时,它永远不会被调用。如果将监听器设置为 ImageView 和/或 TextView,我只能收到点击。单击按钮时,这会导致两种可能的效果:

  1. 点击在 ImageView 或 TextView 内。单击已注册正常,但按钮状态可绘制不会更改,即它不会显示为按下状态。
  2. 点击位于按钮的“空白区域”内。点击未注册,但可绘制状态工作正常。

这两个都不可行。我在 LinearLayout 或其子项上使用了以下属性,但无论是真还是假,似乎都没有任何影响:

  • duplicateParentState
  • 可点击
  • 可聚焦

似乎没有任何合理的方法可以让 LinearLayout 父级代替其子级接收点击。我已经看到一些可能的解决方案覆盖了自定义组件本身的 dispatchTouchEvent 或 onInterceptTouchEvent,但是如果我必须开始分析触摸事件以识别正确的点击,那看起来真的是一团糟。

那么 LinearLayout 上的 OnClickListener 和子节点 = 不行吗?

编辑:这就是我目前将按钮插入 Fragment 主布局的方式。

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

...

<com.package.CustomIconButton
android:id="@+id/someButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
mynamespace:buttonIcon="@drawable/someIcon"
mynamespace:text="@string/str_someText" />

...

按钮在Fragment的代码中绑定(bind)如下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main, container, false);

View customButton = v.findViewById(R.id.goButton);
customButton.setOnClickListener(onClickListener);

...

最佳答案

你确定你在正确的 LinearLayout 上设置了监听器吗(因为你有两个,父级和膨胀布局中的一个)。前段时间我做了一个类似的组件,监听器工作没有问题(我不知道你有那些风格)。唯一的区别是我没有添加(不需要的)LinearLayout,而是使用了 merge 标签:

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

<ImageView
android:id="@+id/buttonIcon"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/buttonText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/white" />

</merge>

并在父 LinearLayout 上设置样式。

关于android - LinearLayout 作为自定义按钮,OnClickListener 从不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12234543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com