如何激活集中的ViewGroup里一个TextView选取框? [英] How to activate a marquee for a TextView inside a focused ViewGroup?

查看:174
本文介绍了如何激活集中的ViewGroup里一个TextView选取框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/first_tv_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:background="@android:drawable/list_selector_background">
        <TextView
            android:id="@+id/first_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            android:singleLine="true"
            android:ellipsize="marquee"
            android:duplicateParentState="true"/>
    </RelativeLayout>
    <TextView
        android:id="@+id/second_tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:background="@android:drawable/list_selector_background"/>
</LinearLayout>

在second_tv获得焦点,如预期second_tv的字幕被激活。当first_tv_container获得焦点,我想first_tv的选取框成为好。我以为加入duplicaParentState = true来first_tv会做的伎俩,但事实并非如此。所以我的问题是:有没有一个标准的,简单的方法有一个TextView的选取框聚焦ViewGroup中(不是一个ListView,它会自动正确处理的情况除外)内工作,并且不延长并覆盖了很多东西。

When second_tv gets the focus, second_tv's marquee is activated as expected. When first_tv_container gets the focus, I would like first_tv's marquee to be as well. I thought adding duplicaParentState=true to first_tv would do the trick, but it doesn't. So my question would be: is there a standard, easy way to have a TextView's marquee working inside a focused ViewGroup (other than a ListView, which automatically handles the case correctly) without extending and overriding a lot of stuff?

问候

推荐答案

OK,我发现某种方法。我添加机器人:可聚焦=true将first_tv,并实例first_tv_container为RelativeLayout的扩展版本中,我宣布descendantFocusability =blocksDescendants并定义以下覆盖:

OK, I've found some kind of way. I added android:focusable="true" to first_tv, and instantiated first_tv_container as an extended version of RelativeLayout in which I declare descendantFocusability="blocksDescendants" and define the following override:

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
{
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    setSelected(gainFocus);
    dispatchSetSelected(gainFocus);
}

正是这种东西我想避免,但将现在做的。

Exactly the kind of stuff I wanted to avoid, but that will do for now.

这篇关于如何激活集中的ViewGroup里一个TextView选取框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆