问题
I want to create a TextView that scroll from right to left and then disappears from left and reappears from right again. I can use animation ?? Thanks
回答1:
I believe you want your text view to marquee. If so, this is how I did it:
In the XML. Set the following attributes for the TextView:
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="false"
android:scrollHorizontally="true"
If your TextView is within a RelativeLayout, the width or height will have to be static (i.e. 32dp). If you need dynamic, place the TextView in another View within the RelativeLayout.
In onCreate(), you need to make the TextView selected:
myTextView.setSelected(true);
Original Answer
回答2:
Does this help? Found it here
<LinearLayout android:orientation="vertical">
<HorizontalScrollView>
<LinearLayout android:orientation="horizonal">
<Image1 />
<Image2 />
<Image3 />
<Image4 />
<Image5 />
</LinearLayout>
</HorizontalScrollView>
<LinearLayout android:orientation="horizonal">
<Button1 android:layout_weight="1" />
<Button2 android:layout_weight="1" />
<Button3 android:layout_weight="1" />
<Button4 android:layout_weight="1" />
</LinearLayout >
回答3:
If you want scroll text while control is focused use
<TextView ... android:ellipsize="marquee" android:singleLine="true"/>
Otherwise you should implement this yourself:
- Using timer (or Handler) and setting offset to TextView
- Stole this
- Or maybe try this
回答4:
I use this code to animate my textView in recyclerView. (this code can use any whree)
in xml
<HorizontalScrollView
android:id="@+id/scrollViewTxtAppsEn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scrollbars="none"
android:visibility="visible"
>
<TextView
android:id="@+id/txtAppsEn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="8dp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:maxLines="1"
android:scrollHorizontally="true"
android:text=""
android:textColor="@color/textColorDark"
android:textSize="9sp"
/>
</HorizontalScrollView>
In adapter
@Override
public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
handelAnimationTextView(holder.txtAppsEn, holder.scrollViewTxtAppsEn, true);
super.onViewAttachedToWindow(holder);
}
private void handelAnimationTextView(TextView textView, HorizontalScrollView scrollViewTextView, boolean isRightToLeft) {
textView.post(new Runnable() {
@Override
public void run() {
if (canScroll(scrollViewTextView)) {
if (isRightToLeft) {
textView.startAnimation((Animation) AnimationUtils.loadAnimation(G.currentActivity, R.anim.text_view_anim_right_to_left));
} else {
textView.startAnimation((Animation) AnimationUtils.loadAnimation(G.currentActivity, R.anim.text_view_anim_left_to_right));
}
}
}
});
}
private boolean canScroll(HorizontalScrollView horizontalScrollView) {
View child = (View) horizontalScrollView.getChildAt(0);
if (child != null) {
int childWidth = (child).getWidth();
return horizontalScrollView.getWidth() < childWidth + horizontalScrollView.getPaddingLeft() + horizontalScrollView.getPaddingRight();
}
return false;
}
text_view_anim_left_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:fromXDelta="-100%"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="100%"/>
text_view_anim_right_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:fromXDelta="100%"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-100%"/>
Hope I have helped
来源:https://stackoverflow.com/questions/9423301/android-textview-horizontally-scrolling