Android 在同一LinearLayout中更改另一个TextView时,TextView将重新启动Marquee
我有LinearLayout和inside 2文本视图都有字幕,当我更新第一个文本时,第二个文本视图会重新启动字幕Android 在同一LinearLayout中更改另一个TextView时,TextView将重新启动Marquee,android,android-linearlayout,marquee,Android,Android Linearlayout,Marquee,我有LinearLayout和inside 2文本视图都有字幕,当我更新第一个文本时,第二个文本视图会重新启动字幕 <LinearLayout android:id="@+id/panel" android:layout_width="320dp" android:layout_height="match_parent" android:l
<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\u width=“320dp”
效果不会发生。
但是我想设置android:layout\u width=“match\u parent”是否有一些解决方法
我发现了类似的问题,但没有解决方案:
我遇到了类似的问题,解决方法是为Textview设置固定大小 那么为什么不按程序进行呢?就我而言,它解决了问题。以下是我的详细解决方案: 布局有点复杂,有很多变化的值。以下是有趣的部分: 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>
最后是活动。正如我前面所说,您需要设置固定的宽度和高度,这样我们就可以通过onCreate()中的侦听器以编程方式进行设置:
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);
}
});
}
@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
}
如果你需要改变方向或诸如此类的事情,要小心,但这对我来说非常有效
----为API-11之前版本编辑---
由于OnLayoutChangeListener
仅存在于Api v11中,因此有一个变通方法(它可以工作,但我认为它不太好):
从活动中删除OnLayoutChangeListener
:
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);
}
});
}
@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
:
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;
}
}
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包装每个字幕文本视图(如果使用RelativeLayout,这将不起作用) 只是一个简单的修复…:)不需要太担心…只需将textview的宽度修复为800dp或更高的宽度。它将解决重置问题您救了我的命。只需将文本视图的宽度设置为特定的dp值,而不是使用
wrap\u content
。我失去了我的早晨,找到了这个。完美的最佳答案。解决问题的最简单、最合乎逻辑的方法。这应该是公认的答案。这完全有效。我只是把我的文本视图
放在线性布局中
。