在同一 LinearLayout 中更改另一个 TextView 时,TextView 会重新启动 Marquee [英] TextView restarts Marquee when changing another TextView in same LinearLayout

查看:23
本文介绍了在同一 LinearLayout 中更改另一个 TextView 时,TextView 会重新启动 Marquee的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 LinearLayout 和里面的 2 TextView 都有选框,当我在第一个更新文本然后第二个 TextView 重新启动选框.

I have LinearLayout and inside 2 TextView both have marquee and when I update text in first then second TextView restarts marquee.

            <LinearLayout
                android:id="@+id/panel"
                android:layout_width="320dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/first"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:gravity="bottom|center_horizontal"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:singleLine="true" />

                <TextView
                    android:id="@+id/second"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:gravity="top|center_horizontal"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:singleLine="true"
                      />
            </LinearLayout>

我发现如果对于 R.id.firstR.id.second 我设置了 layout_width="320dp" 效果不't 发生.但我想设置 android:layout_width="match_parent" 有一些解决方法吗?

I found that if for R.id.first and R.id.second i set layout_width="320dp" the effect don't occurs. But I want to set android:layout_width="match_parent" there is some workaround?

我发现了类似的问题,但没有解决方案:Android,RelativeLayout 在更改 ImageView 时重新启动 Marquee-TextView相对布局

I found similar problem but without solution: Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout

推荐答案

我遇到了类似的问题,解决方案是为 Textview 设置固定大小.

I had a similar problem and the solution IS to set fixed size for the Textview.

那么为什么不按程序进行呢?就我而言,它解决了问题.这是我的详细解决方案:

So why not do it progammatically? In my case, it solved the problem. Here is my solution in details :

布局有点复杂,有很多变化的值.这是有趣的部分:

The layout is a bit complex with a lot of changing values. Here is the interesting part :

layout.xml :

layout.xml :

<!-- The height and visibility values change programatically -->
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="30dp" 
    android:layout_alignParentBottom="true"
    android:visibility="gone" >

    <FrameLayout>
        ...
        // some code
        ...
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" />

        <!-- My scrolling textview. see below. -->
        <!-- The size will be set when -->
        <!-- the layout will be draw, -->
        <!-- after the Activity.onCreate(). -->
        <!-- I removed ALL THE UNECESSARY (I mean  -->
        <!-- scrollHorizontally, focusable and focusableInTouchMode. -->
        <!-- You don't need it !!!!) -->
        <fr.cmoatoto.android.widget.ScrollingTextView 
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever" />

        <ImageView
            ...
            // some code
            ... />
    </LinearLayout>
</RelativeLayout>

ScrollingTextView 已在此 answer

The ScrollingTextView has been defined in this answer

又来了:

ScrollingTextView.java :

ScrollingTextView.java :

public class ScrollingTextView extends TextView {

    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollingTextView(Context context) {
        super(context);
    }

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

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

最后是活动.正如我之前所说,您需要设置固定的宽度和高度,因此我们将使用 onCreate() 中的侦听器以编程方式进行设置:

And finally the Activity. As I said before, You need to set fixed width and height so we will do it programmatically with a listener in the onCreate() :

MyActivity.java :

MyActivity.java :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout);


    TextView textView = ((TextView) findViewById(R.id.home_trafic_text));
    textView.setText(getString(R.string.loading));
    textView.setEnabled(true); // Thanks to Romain Guy
    textView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right,
                int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            LayoutParams params = v.getLayoutParams();
            params.width = right - left;
            params.height = bottom - top;
            params.weight = 0;
            v.removeOnLayoutChangeListener(this);
            v.setLayoutParams(params);
        }
    });
}

如果你需要改变方向或类似的东西,请小心,但它对我来说效果很好!

Be careful if you need to change orientation or things like that but it works pretty well for me !

----为 PRE-API-11 编辑---

因为 OnLayoutChangeListener 只存在于 Api v11,所以有一个解决方法(它有效,但我认为它不太好):

Because OnLayoutChangeListener exists only from Api v11, there is a workaround (It works but I think it is less good) :

从您的活动中删除 OnLayoutChangeListener :

Remove the OnLayoutChangeListener from your activity :

MyActivity.java :

MyActivity.java :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout);

    TextView textView = ((TextView) findViewById(R.id.home_trafic_text));
    textView.setText(getString(R.string.loading));
    textView.setEnabled(true); // Thanks to Romain Guy
}

并在您的 ScrollingTextView 中添加一个 onSizeChanged :

and add a onSizeChanged in your ScrollingTextView :

ScrollingTextView.java :

ScrollingTextView.java :

public class ScrollingTextView extends TextView {

    ...
    // Same code as before
    ...

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    LayoutParams params = (LayoutParams) getLayoutParams();
        params.width = w;
        params.height = h;
        params.weight = 0;
        setLayoutParams(params);
    }
}

希望能帮到你!

这篇关于在同一 LinearLayout 中更改另一个 TextView 时,TextView 会重新启动 Marquee的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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