Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout

北战南征 提交于 2019-12-04 03:48:24

问题


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);
    }
}

回答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:)




回答2:


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.




回答3:


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



回答4:


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!!" />


来源:https://stackoverflow.com/questions/8203286/android-relativelayout-restarts-marquee-textview-when-changing-imageview-in-sam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!