Android,RelativeLayout在同一RelativeLayout中更改ImageView时重新启动Marquee-TextView

发布于 2024-12-16 19:43:19 字数 3270 浏览 0 评论 0原文

我还没有找到解决我的问题的方法,也许你可以在这里帮助我。

我使用带有 ImageView 和 TextView 作为子项的relativelayout。 TextView 包含大文本,应从右向左滚动。但是每次当我向 ImageView 设置新图像时,选取框都会从头开始。

我认为通过设置新图像,TextView 会失去焦点,因此选取框再次开始。我怎样才能防止这种情况发生,或者我还做错了什么?如果有人能指出我正确的解决方案,那就太好了。

多谢!

您可以使用以下代码重现我的问题:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.view.Display;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RelativeLayoutActivity extends Activity {

    private ImageView mImageView;
    private TextView mTextView;

    private Handler mHandler = new Handler();
    private int mCurrentImage = 0;

    private Runnable mCallback = new Runnable() {
        @Override
        public void run() {
            toggleImage();
        }
    };

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

        setContentView(R.layout.main);

        initLayout();
        setLongText();

        mHandler.postDelayed(mCallback, 5000);
    }

    private void initLayout() {
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.parentLayout);

        Display display = getWindowManager().getDefaultDisplay(); 
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        mImageView = new ImageView(this);
        mImageView.setScaleType(ScaleType.FIT_XY);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(screenWidth,screenHeight);        
        mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        layout.addView(mImageView, rlp);

        // marquee text at the bottom
        mTextView = new TextView(this);
        mTextView.setSingleLine();
        mTextView.setEllipsize(TruncateAt.MARQUEE);
        mTextView.setMarqueeRepeatLimit(-1);
        mTextView.setHorizontallyScrolling(true);
        mTextView.setFocusable(true);
        mTextView.setFocusableInTouchMode(true);
        mTextView.setBackgroundColor(Color.BLACK);
        mTextView.setTextColor(Color.WHITE);

        rlp = new RelativeLayout.LayoutParams(screenWidth, 50);
        rlp.topMargin = screenHeight-100;
        layout.addView(mTextView, rlp);
    }

    private void setLongText() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<50; i++) {
            sb.append(" ");
        }
        for (int i=0; i<50; i++) {
            sb.append("A");
        }
        for (int i=0; i<50; i++) {
            sb.append("B");
        }
        for (int i=0; i<50; i++) {
            sb.append("C");
        }
        mTextView.setText(sb.toString());
        mTextView.setSelected(true);
    }

    private void toggleImage() {
        mCurrentImage++;
        if (mCurrentImage % 2 == 0) {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        } else {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.green));
        }

        mHandler.postDelayed(mCallback, 5000);
    }
}

I have not found a solution for my problem, maybe you can help me here.

I am using a RelativeLayout with an ImageView and a TextView as children. The TextView contains a large text and should scroll from right to left. But everytime when I set a new image to the ImageView, the marquee starts from beginning.

I think that by setting a new image the TextView looses its focus and so the marquee starts again. How can I prevent that or is there something else I am doing wrong? Would be great if someone could point me to the correct solution.

Thanks a lot!

You can reproduce my problem with this code:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.view.Display;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RelativeLayoutActivity extends Activity {

    private ImageView mImageView;
    private TextView mTextView;

    private Handler mHandler = new Handler();
    private int mCurrentImage = 0;

    private Runnable mCallback = new Runnable() {
        @Override
        public void run() {
            toggleImage();
        }
    };

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

        setContentView(R.layout.main);

        initLayout();
        setLongText();

        mHandler.postDelayed(mCallback, 5000);
    }

    private void initLayout() {
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.parentLayout);

        Display display = getWindowManager().getDefaultDisplay(); 
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        mImageView = new ImageView(this);
        mImageView.setScaleType(ScaleType.FIT_XY);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(screenWidth,screenHeight);        
        mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        layout.addView(mImageView, rlp);

        // marquee text at the bottom
        mTextView = new TextView(this);
        mTextView.setSingleLine();
        mTextView.setEllipsize(TruncateAt.MARQUEE);
        mTextView.setMarqueeRepeatLimit(-1);
        mTextView.setHorizontallyScrolling(true);
        mTextView.setFocusable(true);
        mTextView.setFocusableInTouchMode(true);
        mTextView.setBackgroundColor(Color.BLACK);
        mTextView.setTextColor(Color.WHITE);

        rlp = new RelativeLayout.LayoutParams(screenWidth, 50);
        rlp.topMargin = screenHeight-100;
        layout.addView(mTextView, rlp);
    }

    private void setLongText() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<50; i++) {
            sb.append(" ");
        }
        for (int i=0; i<50; i++) {
            sb.append("A");
        }
        for (int i=0; i<50; i++) {
            sb.append("B");
        }
        for (int i=0; i<50; i++) {
            sb.append("C");
        }
        mTextView.setText(sb.toString());
        mTextView.setSelected(true);
    }

    private void toggleImage() {
        mCurrentImage++;
        if (mCurrentImage % 2 == 0) {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        } else {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.green));
        }

        mHandler.postDelayed(mCallback, 5000);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沦落红尘 2024-12-23 19:43:19

我有一个同样的问题,我刚刚修复了它:)

如果你的 TextView 在布局 XML 中包含layout_weight

android:layout_weight="1"

删除它!这个属性会导致选框重新启动。

希望有帮助:)

I had a same problem and I fixed it just now:)

If your TextView which in layout XML contains layout_weight

android:layout_weight="1"

Remove it!This attibute cause marquee restart.

Hope helpful:)

触ぅ动初心 2024-12-23 19:43:19

选取框重置问题是由于运行它的 TextView失去焦点造成的。要解决这些问题,您可以简单地用另一个 RelativeLayout 包围您的 TextView,以便它与您的其他视图位于不同的 ViewGroup 中。

您还有另一种选择:创建一个新类,它是 TextView 的子类,并覆盖 onFocusChangedonWindowFocusChanged 以防止所述文本视图失去焦点。

就是这样。使用这些技术,每次另一个元素获得焦点时,您的选取框就不会重新启动。

The marquee reset problem is because of loss of focus of the TextView on which it is running. To overcome these issues you can simply surround your TextView with another RelativeLayout so that it is in a different ViewGroup as your other views.

You have another option: Create a new class that is a subclass of TextView and override onFocusChanged and onWindowFocusChanged to prevent loss of focus for the said textview.

That's it. Using these techniques your marquee won't restart every time another element gains focus.

撩心不撩汉 2024-12-23 19:43:19

在这里查看我的答案: https://stackoverflow.com/a/13841982/1823503

这个想法是:

  • 固定宽度和高度以编程方式(你做到了)
  • 来 setSelected(你做到了)
  • 来覆盖你的 TextView 来解决焦点问题

See my answer here : https://stackoverflow.com/a/13841982/1823503

The idea is :

  • to fix the width and height programmatically (you did it)
  • to setSelected (you did it)
  • to Override your TextView to fix the focus problems
〆凄凉。 2024-12-23 19:43:19

只是一个简单的修复..:) 无需担心太多...只需将 textview 的宽度修复为 800dp 或过高的宽度即可。它将解决重置问题

    <TextView
    android:id="@+id/adv_txt_view"
    android:layout_width="800dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="marquee"                               

    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"                           
    android:focusableInTouchMode="true"                           
    android:focusable="true"                     
    android:scrollHorizontally="true"                       
    android:textColor="@color/black"
    android:text="large text to scroll!!" />

just a simple Fix..:) no need to worry much...just fix width of textview as some 800dp or too higher width. it will solve reset issue

    <TextView
    android:id="@+id/adv_txt_view"
    android:layout_width="800dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="marquee"                               

    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"                           
    android:focusableInTouchMode="true"                           
    android:focusable="true"                     
    android:scrollHorizontally="true"                       
    android:textColor="@color/black"
    android:text="large text to scroll!!" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文