我在 FrameLayout 中有一个 TextView,如下所示:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
... >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
... />
TextView 的文本是在代码中设置的,我在 TextView 上运行了一个翻译动画,它在父 View 中向左滑动,如下所示:
myTextView.setText(getVenueInformationText());
myTextView.setAnimation(AnimationUtils.loadAnimation(myContext, R.anim.slide_left_repeatedly));
我需要满足 TextView 中的文本比父 View (在本例中为 FrameLayout)的宽度更短或的两种情况。但是,我发现动画 TextView 的宽度仅与 FrameLayout 一样宽(因此我的文本被切碎了)。
有谁知道为什么当我在我的 TextView 上设置文本并设置动画时,TextView 显示的宽度只与 FrameLayout 一样宽(即使 TextView 中的文本应该将其拉伸(stretch)到比 FrameLayout 更宽)?
Note 1: A marquee animation on the TextView won't do the job for me as it is possible for the text in the TextView to be smaller than the width of the parent View (in which case the marquee animation does not run... and I need the animation to run regardless of the text's width).
Note 2: Below is the xml definition of the translate animation I am using:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fromXDelta="100%p"
android:toXDelta="-100%"
android:repeatCount="infinite"
android:duration="15000" />
最佳答案
通过将我的 TextView 放在 HorizontalScrollView(而不是 FrameLayout 或 LinearLayout)中来解决它,如下...
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:scrollbars="none" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" />
</HorizontalScrollView>
...然后像以前一样以编程方式/动态方式设置 TextView 的文本和动画。
关于android - 如何指定一个 View 的宽度来包裹内容并且可能比它的父级更宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9067164/