简体   繁体   English

Android RelativeLayout可点击叠加视图

[英]Android RelativeLayout clickable overlay view

I'm changing a layout to use a RelativeLayout rather than complex nested LinerLayouts - but I've run into some trouble with getting a OnClickListener for a View to work. 我正在更改布局以使用RelativeLayout而不是复杂的嵌套LinerLayouts-但是在使OnClickListener使View正常工作时遇到了一些麻烦。

I've tried the usual stuff - bring to front, set clickable, setting focusable/focusableInTouchMode to true and false, nothing seems to be working for me. 我已经尝试了常见的东西-摆在最前面,设置clickable,将focusable / focusableInTouchMode设置为true和false,似乎对我没有任何帮助。 Hopefully I've just made a silly mistake and you guys can help me out :D 希望我犯了一个愚蠢的错误,你们可以帮助我:D

Here's my code: 这是我的代码:

Layout: < removed, see edit > 布局:<已删除,请参见编辑>

Custom base adapter: < removed, turns out its just a layout issue > 自定义基本适配器:<已删除,原来只是布局问题>

The layout is for a row item in a list adapter, and I need to make only half of the row clickable, as the other half has it's own toggles (which are working correctly). 布局用于列表适配器中的行项目,我只需要使行的一半可单击,因为另一半具有它自己的开关(它们可以正常工作)。

Thanks in advance for any suggestions!! 在此先感谢您的任何建议!!

EDIT: This is strange - I changed it from View to TextView, set a background colour, and now it's showing up - but it's not following the relative layout settings: 编辑:这很奇怪-我将其从“视图”更改为“文本视图”,设置背景色,现在显示出来-但它没有遵循相对的布局设置:

        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/toggle"

Specifically it doesn't align with the top as it should. 具体来说,它不应该与顶部对齐。 But the clicking works! 但是点击有效! Now to just get it to cover all the way to the top... 现在,让它覆盖一路攀登至顶...

EDIT2: 编辑2:

Updated layout code: 更新的布局代码:

< removed, see later edit > <已删除,请参阅稍后编辑>

EDIT3: 编辑3:

The orange is clickView - I want it to extend to the top of each row. 橙色是clickView-我希望它扩展到每一行的顶部。

在此处输入图片说明

EDIT4: (changed title) EDIT4 :(标题更改)

Ok I've got it working properly now, with the clickView covering the row top to bottom of the left side (and without using specific height/widths). 好的,我现在可以正常使用它了,单击视图覆盖了左侧的从上到下的行(并且没有使用特定的高度/宽度)。
Here's the current layout code, let me know if there's something I should be doing differently/any improvements you can think of! 这是当前的布局代码,请让我知道我是否应该做一些不同的事情/可以想到的任何改进!

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

    <View android:id="@+id/fakeView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true"/>

    <TextView android:id="@+id/clickView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/toggle"
        android:layout_alignBottom="@+id/pokemonTypes" />
       <!-- android:background="#ffff914e" -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="001"
        android:id="@+id/pokemonNumber"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Bulbasaur"
        android:id="@+id/pokemonName"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/pokemonNumber"
        android:layout_alignRight="@+id/fakeView"
        android:layout_marginLeft="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Grass"
        android:id="@+id/pokemonTypes"
        android:layout_below="@+id/pokemonName"
        android:layout_toRightOf="@+id/pokemonNumber"
        android:layout_alignRight="@+id/fakeView"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/pokemonImage"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/fakeView" />

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/check1"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/toggle2" />

    <ToggleButton
        android:id="@+id/toggle2"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/check2"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/toggle3" />

    <ToggleButton
        android:id="@+id/toggle3"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/check3"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

One other thing I'd like to do - add a larger space to the right of the toggles (margin possibly?) so that when scrolling the fast scroll bar doesn't cover the toggle. 我想做的另一件事-在切换器的右侧添加一个较大的空间(可能有余量?),以便在滚动快速滚动条时不会覆盖切换器。 Any ideas? 有任何想法吗?

This is a sample layout based on your responses. 这是根据您的回答设计的样本。 It is not exactly like your original layout, but I try to make it similar when applicable. 它与您的原始布局不完全一样,但是在适用时,我尝试使其相似。 Please be careful and compare what is missing as a difference. 请小心并比较缺少​​的内容。 It looks almost perfect in the Designer. 在设计器中看起来几乎是完美的。

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

    <View
        android:id="@+id/fakeView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/clickView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignBottom="@id/info"
        android:layout_alignTop="@id/info"
        android:background="#ffff914e" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:id="@+id/pokemonNumber"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_margin="5dp"
            android:text="001"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:orientation="vertical">

        <TextView
            android:id="@+id/pokemonName"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/fakeView"

            android:layout_marginLeft="5dp"
            android:layout_toEndOf="@+id/pokemonNumber"
            android:layout_toRightOf="@+id/pokemonNumber"
            android:text="Bulbasaur"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/pokemonTypes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"

            android:layout_marginStart="5dp"
            android:text="Grass"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        </LinearLayout>

        <ImageView
            android:id="@+id/pokemonImage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignLeft="@+id/fakeView"
            android:layout_alignParentTop="true"
            android:layout_gravity="center" />

        <ToggleButton
            android:id="@+id/toggle"
            android:layout_width="25dp"
            android:layout_height="25dp"

            android:text="Toggle"

            android:textOn=""
            android:textOff=""
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp" />

    </LinearLayout>

</RelativeLayout>

Notes: 笔记:

  1. A nested LinearLayout is used to set some UI elements in a horizontal orientation/view. 嵌套的LinearLayout用于在水平方向/视图中设置一些UI元素。 And I gave it an ID for reference. 我给了它一个ID供参考。
  2. For clickView, layout_alignBottom and layout_alignTop is used. 对于clickView,使用layout_alignBottom和layout_alignTop。 I think layout_alignParentTop and its bottom is useless for your needs. 我认为layout_alignParentTop及其底部对于您的需求没有用。
  3. The layout_height of clickView is wrap_content , no need to set a specific height which may not look good in different screen resolutions. clickView的layout_heightwrap_content ,无需设置特定的高度,该高度在不同的屏幕分辨率下可能效果不佳。
  4. The Textview pokemonTypes is after the LinearLayout so that it is vertical, relative to pokemonName. Textview pokemonTypes在LinearLayout之后,因此相对于pokemonName是垂直的。

Overall the layout may not be perfect to your specifications. 总体而言,布局可能不符合您的要求。 I think it's a good start. 我认为这是一个好的开始。

Another good idea is to use layout_weight attribute but so far I think you don't need it. 另一个好主意是使用layout_weight属性,但到目前为止,我认为您不需要它。 layout_weight avoids using specific values. layout_weight避免使用特定的值。 Remember I try to prevent using specific values, hoping to avoid issues with screen resolutions. 请记住,我尝试避免使用特定的值,以免出现屏幕分辨率问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM