【问题标题】:Clickable layout with inner views behavior issue带有内部视图行为问题的可点击布局
【发布时间】:2014-04-29 04:16:50
【问题描述】:

我正在测试创建复合视图,因此我从包含 ImageView 和 TextView 的线性布局开始。这些 LinearLayout 将表示类似于部分列表的视图,并且我将其设置为可点击的。布局的内部视图具有选择器作为其颜色(TextView)和图像(ImageView)。在 API 19 (KitKat) 中,点击行为正常(屏幕截图 1),但在 API 15 和 API 10(屏幕截图 2)中,点击并没有改变选择器状态。

每个“列表项”的代码是:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_login"
    android:clickable="true"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="15dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:src="@drawable/signin_signup_icon_facebook" />

    <TextView
        style="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Facebook"
        android:textColor="@color/option_text_color" />
 </LinearLayout>

编辑:我使用了触摸侦听器方法。如果有一天我找到了最好的解决方案(没有 Java 代码),我会在这里进行编辑。我的方法,基于答案(将 View.OnTouchListener 添加到 LinearLayouts):

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    ViewGroup container = (ViewGroup) view;
    boolean pressed;
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) pressed = true;
    else if (motionEvent.getAction() == MotionEvent.ACTION_UP) pressed = false;
    else return false;

    for (int i = 0; i < container.getChildCount(); i++) {
        container.getChildAt(i).setPressed(pressed);
    }

    return false;
}

在旧的 2.3 Android 中,代码运行速度有点慢,但也能正常工作。

【问题讨论】:

  • 请发布您的适配器文件代码
  • 这不是一个列表。这是一个带有内部 LinearLayouts 的 LinearLayout
  • 查看我的更新答案

标签: java android android-layout


【解决方案1】:

我尝试了不同的 API,但遇到了同样的问题。我认为旧的 API 可能没有这个功能,它允许内部视图的外观响应它的父视图的点击方法。

您可以使用 touchListenr 在代码中更改内部视图的外观,或者您可以尝试不使用 LinearLayout 而只使用带有图片的 TextView,使用 android:drawableLeft,然后您可以在同一时间。

编辑: 像这样使用 onTouchListener:

@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    imageView.setPressed(true);
    textView.setPressed(true);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
    imageView.setPressed(false);
    textView.setPressed(false);
}
return false;
} 

希望对你有帮助。

【讨论】:

  • 我没有使用复合drawable的原因是,如果我在TextView中使用wrap_content,背景没有填满所有宽度,如果我使用match_parent,图像总是对齐屏幕左侧...
  • 是的……它有这个问题。只需用 Java 代码编写它,就可以了,它并没有那么复杂。附言。如果您找到没有 Java 的解决方案,请告诉我 ^_^ @Rafael
【解决方案2】:

你不应该对同一种布局使用 4 个布局,更好的方法是使用 listview。但这也可以这样解决:

为线性布局设置 ontouchlistner

case MotionEvent.ACTION_DOWN:
//change image view (white image) and text color(white) and linear layout background color(Red) here
  break;
case MotionEvent.ACTION_UP:
//change image view (red image) and text color(red) and linear layout background color(white) here
  break;

这将解决您的问题,并且肯定会像魅力一样工作,或者您可以使用线性布局像按钮一样工作并为布局设置选择器,如下所示: Making a LinearLayout act like an Button

【讨论】:

  • 使用 LinearLayouts 的原因是列表可能无法滚动。没有可滚动列表的建议是使用 LinearLayout,不是吗?
  • 嗯!行!我的解决方案对您有帮助吗?
【解决方案3】:

试试这样的:

您的 Xml 文件

    <LinearLayout
    android:id="@+id/linearLaout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_login"
    android:clickable="true"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="15dp">

    <ImageView
        android:id="@+id/iamge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:src="@drawable/signin_signup_icon_facebook" />

    <TextView
        android:id="@+id/textView"
        style="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Facebook"
        android:textColor="@color/option_text_color" />
 </LinearLayout>

只需单击一下即可更改线性布局颜色的背景 并更改图像视图的背景并更改文本视图的颜色。其他保持不变。

检查链接是否完全像选择器那样使用Android LinearLayout Selector background color

【讨论】:

  • 同时发布您的 java 文件
  • 没有 Java 代码。我只在片段中从 XML 膨胀
猜你喜欢
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多