有没有一种方法,使ellipsize =“跑马灯”总是滚动?
题
我想使用一个TextView字幕效果,但文本时TextView的获得焦点只被滚动。这是一个问题,因为在我的情况下,它不能。
我使用:
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
有没有办法有TextView的滚动总是它的文本?我已经看到了这个被在Android Market的应用程序,在应用程序名称会在标题栏滚动,即使它不接收焦点做,但我找不到这被API文档提到的。
解决方案
我一直面临的问题,并在最短的解决方案,我已经想出是创建的TextView衍生的新类。 类应该重写三种方法的 onFocusChanged 强>,<强> onWindowFocusChanged 和<强> isFocused 以使所有的TextView聚焦。
@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;
}
其他提示
我今天终于碰到了这个问题,并因此解雇了 hierarchyviewer
在Android Market应用程序。
寻找在应用的详细信息画面上的标题,它们使用一个普通的旧TextView
。检查其特性表明,它不集中,不能被聚焦并且是通常很普通的。 - 除了事实,即它被标记为的选择强>
和我有它的工作:)
textView.setSelected(true);
这是有道理的,因为的Javadoc说什么一个>:
一个视图可以被选择或不。请注意,选择不一样的焦点。视图在像的ListView或GridView的一个AdapterView的上下文中通常选择。
即。当您在滚动中的项目列表视图(如在市场应用程序),最后才在现在选择的文字开始滚动。而且,由于这种特殊的TextView
不可作为焦点,或点击,它永远不会失去其选择状态。
不幸的是,据我所知是没有办法预先设定从布局XML选中状态。结果 但是,上面的一行代码工作正常,我。
只要把这些参数在你的TextView。它的工作原理:)
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
TranslateAnimation
的工作原理是“拉”的在一个方向上按指定的量视图。可以设置从哪里开始这个“拉”以及在哪里结束。
TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
fromXDelta设置运动的在X轴的起始位置的偏移量。
fromXDelta = 0 //no offset.
fromXDelta = 300 //the movement starts at 300px to the right.
fromXDelta = -300 //the movement starts at 300px to the left
toXDelta限定在X轴移动的偏移量结束位置。
toXDelta = 0 //no offset.
toXDelta = 300 //the movement ends at 300px to the right.
toXDelta = -300 //the movement ends at 300px to the left.
如果您的文本的宽度是大于fromXDelta和toXDelta之间的差的模块,文本将不能够完全以和在画面内移动compeltely
实施例
让我们假设我们的屏幕尺寸为320x240 P-XS。我们必须与具有700像素宽度的文字一个TextView,我们希望创建一个“拉”的文字动画,使我们可以看到这句话的结尾。
(screen)
+---------------------------+
|<----------320px---------->|
| |
|+---------------------------<<<< X px >>>>
movement<-----|| some TextView with text that goes out...
|+---------------------------
| unconstrained size 700px |
| |
| |
+---------------------------+
+---------------------------+
| |
| |
<<<< X px >>>>---------------------------+|
movement<----- some TextView with text that goes out... ||
---------------------------+|
| |
| |
| |
+---------------------------+
首先,我们设定fromXDelta = 0
使运动不具有起始偏移。现在,我们需要弄清楚的toXDelta值。为了实现我们需要的“拉”的文本,它跨越了屏幕完全相同的PX预期的效果。 (流程图中,通过<<<< X PX >>>>表示)由于我们的文本有700的宽度,以及可见光区域是320像素(屏幕宽度)我们设定:
tXDelta = 700 - 320 = 380
我们如何计算屏幕的宽度和文本宽度?
代码
服用Zarah片段作为起点:
/**
* @param view The Textview or any other view we wish to apply the movement
* @param margin A margin to take into the calculation (since the view
* might have any siblings in the same "row")
*
**/
public static Animation scrollingText(View view, float margin){
Context context = view.getContext(); //gets the context of the view
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained wisth of the view
float width = view.getMeasuredWidth();
// gets the screen width
float screenWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
// perfrms the calculation
float toXDelta = width - (screenWidth - margin);
// sets toXDelta to 0 if the text width is smaller that the screen size
if (toXDelta < 0) {toXDelta = 0; } else { toXDelta = 0 - toXDelta;}
// Animation parameters
Animation mAnimation = new TranslateAnimation(0, toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
return mAnimation;
}
有可能会执行此更简单的方法,但这个工程对每个视图你能想到的和可重复使用。如果你想动画在ListView一个TextView没有打破的TextView的启用/聚焦状态的能力是特别有用的。它也不断地滚动,即使视图不集中。
我不知道,如果你仍然需要的答案,但我发现一个简单的方法来做到这一点。
设置动画像这样:
Animation mAnimation = new TranslateAnimation(START_POS_X, END_POS_X,
START_POS_Y, END_POS_Y);
mAnimation.setDuration(TICKER_DURATION);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
START_POS_X
,END_POS_X
,START_POS_Y
和END_POS_Y
是float
值,而TICKER_DURATION
是我与其他常量声明的int
。
然后你可以在这个动画现在适用于您的TextView:
TextView tickerText = (TextView) findViewById(R.id.ticker);
tickerText.setAnimation(mAnimation);
这就是它。 :)
我的动画开始右侧离屏(300F),结束左侧离屏(-300°F)中,用15S的持续时间(15000)。
我写下面的代码用于与滚动字幕文本项一个ListView。正是基于上述的setSelected溶液。基本上,我延伸一个ArrayAdapter类并覆盖getView方法返回之前选择的TextView:
// Create an ArrayAdapter which selects its TextViews before returning
// them. This would enable marqueeing while still making the list item
// clickable.
class SelectingAdapter extends ArrayAdapter<LibraryItem>
{
public
SelectingAdapter(
Context context,
int resource,
int textViewResourceId,
LibraryItem[] objects
)
{
super(context, resource, textViewResourceId, objects);
}
@Override
public
View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView textview = (TextView) view.findViewById(
R.id.textview_playlist_item_title
);
textview.setSelected(true);
textview.setEnabled(true);
textview.setFocusable(false);
textview.setTextColor(0xffffffff);
return view;
}
}
这是在我的谷歌搜索的顶部,所以我想我可以在这里发表一个有用的答案,因为我有相当频繁记住这个斗争弹出的答案。 无论如何,这对我的作品,需要XML属性和A的onFocusChangeListener。
//XML
<TextView
android:id="@+id/blank_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal|center_vertical"
android:background="#a4868585"
android:textColor="#fff"
android:textSize="15sp"
android:singleLine="true"
android:lines="1"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
tools:ignore="Deprecated" />
//JAVA
titleText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
titleText.setSelected(true);
}
}
});
// XML
<TextView
android:id="@+id/tvMarque"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:layout_gravity="center_horizontal"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:padding="5dp"
android:textSize="16sp"
android:text=""
android:textColor="@color/colorSyncText"
android:visibility="visible" />
//在Java
mtvMarque.setEllipsize(TextUtils.TruncateAt.MARQUEE);
mtvMarque.setSelected(true);
mtvMarque.setSingleLine(true);