Android 如何反转TextView的选框方向
我想在TextView中反转字幕的方向。默认情况下,文本从右向左移动,我希望它从左向右移动。我如何才能做到这一点?以下是解决方案: 在布局文件中设置此选项:Android 如何反转TextView的选框方向,android,android-layout,android-animation,textview,Android,Android Layout,Android Animation,Textview,我想在TextView中反转字幕的方向。默认情况下,文本从右向左移动,我希望它从左向右移动。我如何才能做到这一点?以下是解决方案: 在布局文件中设置此选项: <TextView android:id="@+id/textId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#337700" android:textSize="20px" andro
<TextView
android:id="@+id/textId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#337700"
android:textSize="20px"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:textStyle="bold"
android:text="Download Form for Different Vehiecle Purposes ....!!!">
</TextView>
在我的例子中,这是从右向左移动文本。您正在寻找的功能此时似乎不可用 您可以从textview.java中的源代码创建自己的反向字幕textview,但其中有很多对“字幕”的引用。我数了50多个,所以反转滚动方向可能需要一些时间 我认为一些双向语言支持可能允许您欺骗textview从左向右滚动,但Android似乎不太支持RTL语言 目前,您唯一的选择是接受字幕的方向,或者创建自己的支持您的功能的TextView类 我将从第3810-3815行看这一部分
if (mMarquee != null && mMarquee.isRunning()) {
canvas.translate(-mMarquee.mScroll, 0.0f);
}
在mMarquee变为之前删除减号:
if (mMarquee != null && mMarquee.isRunning()) {
canvas.translate(mMarquee.mScroll, 0.0f);
}
显然,您需要进行额外的更改,但这将为您指明正确的方向(字面意思)。我建议您使用此行为创建自己的组件 像这样: 具有单个文本视图作为子视图的ViewFlipper(显示文本) 字幕:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="1500"/>
</set>
选取框:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="1500"/>
</set>
您必须稍微调整持续时间,使其看起来像“本地”动画。:-) 我想出了一个简单易行的方法。我做了一个选框效果,根据我们的选择向两个方向移动。所以,这里是诀窍: 我在水平滚动视图中使用了文本视图。我用编程的方式控制它的滚动。我使用以下方法获得了文本长度:
scroll_pos = (int)myTextView.getLayout().getLineWidth(0);
然后我使用Handler并递归调用它,直到达到滚动限制。在这个处理程序中,我使我的HorizontalScrollView滚动到某个位置:
Handler hHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
hScroll.scrollTo(scroll_pos, 0);
scroll_pos--;
if(scroll_pos >= 0)
hHandler.sendEmptyMessage(0);
}
};
在这里,从左到右是一个平滑的选框。干杯 另一个简单的方法是使用HTML,您还可以轻松更改方向
direction=“Left”
对于每个要使用的从右到左的文本: 试试这个:
TextView tv = (TextView) findViewById(R.id.txt);
Rect bounds = new Rect();
Paint textPaint = tv.getPaint();
String text = tv.getText().toString();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();
LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
lp.width = width + 100;
int startX = 300;
TranslateAnimation ta = new TranslateAnimation(startX, lp.width, 0, 0);
ta.setDuration(20000);
ta.setRepeatCount(-1);
tv.setAnimation(ta);
最后,我在这里找到了最佳解决方案: 要使用此库,请执行以下操作:
<your.packagename.MarqueeView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:scrollbars="none"
android:visibility="visible"
android:clickable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test"
android:id="@+id/scrollingtext"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingRight="25dp"
android:paddingLeft="25dp" />
</your.packagename.MarqueeView>
对不起,前面我写错了“从左到右的默认方向”。现在我已经编辑了我的问题。谢谢。我会试试的,我会告诉你进展的,没问题。请记住,ViewFlipper的flipInterval应等于累计持续时间。比如:flipper.setFlipInterval(3000);flipper.startFlipping();请你再解释一下好吗?例如,什么是hScroll
?@khawar您能给我一些链接或参考资料吗?这样我也可以这样做。我需要相同的功能。@khawar——它在第行给出“java.lang.NullPointerException”:scroll_pos=(int)myTextView.getLayout().getLineWidth(0);“有什么想法吗?@HarshTrivedi:找一个全球树观察者,你太快就得到了布局。
<html><body><FONT COLOR="#000000" ><marquee id="mrqSlogan" direction="Left" style="width: auto;" >text your</marquee></FONT></body></html>
webView.loadDataWithBaseURL(null, yourhtmltext, "text/html" , null, null);
TextView tv = (TextView) findViewById(R.id.txt);
Rect bounds = new Rect();
Paint textPaint = tv.getPaint();
String text = tv.getText().toString();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();
LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
lp.width = width + 100;
int startX = 300;
TranslateAnimation ta = new TranslateAnimation(startX, lp.width, 0, 0);
ta.setDuration(20000);
ta.setRepeatCount(-1);
tv.setAnimation(ta);
<your.packagename.MarqueeView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:scrollbars="none"
android:visibility="visible"
android:clickable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test"
android:id="@+id/scrollingtext"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingRight="25dp"
android:paddingLeft="25dp" />
</your.packagename.MarqueeView>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text=" ......... گپ دوستان بیسیبییبب "
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/font_size_xlarge" />