在同一LinearLayout中更改另一个TextView时,TextView重新启动选取框 [英] TextView restarts Marquee when changing another TextView in same LinearLayout
问题描述
我有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.first
和R.id.second
设置为layout_width="320dp"
,则不会发生这种情况.
但是我想设置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,当在同一ImageView中更改ImageView时,RelativeLayout会重新启动Marquee-TextView RelativeLayout
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 !
----针对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
:
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重新启动选取框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!