Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LinearLayout作为自定义按钮,OnClickListener从未调用_Android_Android Layout_Android Custom View - Fatal编程技术网

Android LinearLayout作为自定义按钮,OnClickListener从未调用

Android LinearLayout作为自定义按钮,OnClickListener从未调用,android,android-layout,android-custom-view,Android,Android Layout,Android Custom View,我一直在使用带有图标(drawableTop)和文本的常用Android按钮。如果你想要一个非标准尺寸的按钮,它的效果非常差,所以我决定制作一个带有线性布局的自定义按钮,其布局如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ButtonHoloDark" a

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

<?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>
但当我在父布局中的按钮上设置OnClickListener时,它永远不会被调用。我只能在将侦听器设置为ImageView和/或TextView时接收单击。单击按钮时,可能会产生两种效果:

  • 单击位于ImageView或TextView内。点击被注册为ok,但按钮状态drawable没有改变,即它没有显示为按下状态
  • 点击位于按钮的“空白区域”内。单击未注册,但状态“可绘制”正常
  • 这两者都不可行。我在LinearLayout或其子对象上使用了以下属性,但似乎没有任何属性对其产生任何影响,无论是真是假:

    • 重复父状态
    • 可点击
    • 聚焦
    似乎没有任何合理的方法让LinearLayout父对象而不是其子对象接收单击。我已经看到一些可能的解决方案覆盖了定制组件本身上的dispatchTouchEvent或onInterceptTouchEvent,但是如果我必须开始分析touch事件以确定正确的点击,那么这看起来真的是一团糟

    那么,一旦单击Listener与孩子们的线性布局=禁止

    编辑: 这就是我当前将按钮插入片段主布局的方式

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

    您可以为线性布局设置一个透明子项,并设置其维度
    填充父项
    ,然后为其注册
    onClickListener
    ,因此当用户单击它时,它将响应。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@null" />
    
    </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的OnClickListener不会启动。是的,在按钮的子视图上设置OnClickListener肯定是一个可能的选项。通过使用fill_parent,在不留下任何间隙或扭曲图标的情况下正确放置子对象确实需要更多的努力。如果我不能让LinearLayout自己的click listener工作,我可能会这样做。我不确定我是否遵循了你的建议。如果使用“合并”,如何将按钮视为单个视图?在合并的情况下,父线性布局是什么意思?(我编辑了我的问题以显示我当前如何使用按钮。)@ohra不需要内部
    线性布局。这篇关于合并标签的文章应该有助于您理解。样式必须移动到使用
    CustomIconButton
    (在片段/活动中)布局的布局。好的,谢谢,现在我了解了LinearLayout内部的LinearLayout问题。我将按钮的LinearLayout替换为merge、moved style和一些其他属性,直到主布局,但由于某些原因,按钮现在完全失效。它们在样式方面看起来很好,但是状态可绘制(来自样式)不起作用,并且单击仍然没有注册。我可能还有什么地方不对劲?@ohra我已经做了一个小样本(没有样式附加到
    线性布局
    )关于你的代码和
    OnClickListener
    的工作。你可以在这里找到它。查看您与我发布的代码的不同之处,以及您是否可以进行任何调整(可能样式可能有问题,但应该触发侦听器)。发现问题(我以前的事件调度实验中的一些遗留代码),现在它可以完美地工作。风格包括在内。非常感谢你,@Luksprog。
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@null" />
    
    </LinearLayout>
    
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            ImageView iv = (ImageView) findViewById(R.id.imageView1);
            iv.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.out.println("CLICKED!");
                }
            });
        }
    
    <?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>