Android,RelativeLayout在同一RelativeLayout中更改ImageView时重新启动Marquee-TextView
我还没有找到解决我的问题的方法,也许你可以在这里帮助我。
我使用带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有一个同样的问题,我刚刚修复了它:)
如果你的 TextView 在布局 XML 中包含layout_weight
删除它!这个属性会导致选框重新启动。
希望有帮助:)
I had a same problem and I fixed it just now:)
If your TextView which in layout XML contains layout_weight
Remove it!This attibute cause marquee restart.
Hope helpful:)
选取框重置问题是由于运行它的 TextView失去焦点造成的。要解决这些问题,您可以简单地用另一个
RelativeLayout
包围您的 TextView,以便它与您的其他视图位于不同的 ViewGroup 中。您还有另一种选择:创建一个新类,它是 TextView 的子类,并覆盖
onFocusChanged
和onWindowFocusChanged
以防止所述文本视图失去焦点。就是这样。使用这些技术,每次另一个元素获得焦点时,您的选取框就不会重新启动。
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
andonWindowFocusChanged
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.
在这里查看我的答案: https://stackoverflow.com/a/13841982/1823503
这个想法是:
See my answer here : https://stackoverflow.com/a/13841982/1823503
The idea is :
只是一个简单的修复..:) 无需担心太多...只需将 textview 的宽度修复为 800dp 或过高的宽度即可。它将解决重置问题
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