android - 如何指定一个 View 的宽度来包裹内容并且可能比它的父级更宽?

标签 android android-layout android-animation textview android-view

我在 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 放在 Horizo​​ntalScrollView(而不是 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/

相关文章:

android - 是否可以在 ActionBar 上使用 TransitionDrawable?

java - Android:取出下一张具有钉书钉效果的扑克牌

java - 无法根据从数据库中检索到的值设置微调器位置

java - 为什么 Activity 管理器显示启动 Intent 消息?

android - android 中的 Facebook 对话框不出现

android - 自定义 View 不响应触摸

Android ObjectAnimator 动画 translationY 在所有屏幕尺寸上的比例相同

android - 如何在 Android Object Animator 上获取随机数

java - 为什么我会收到 InvocationTargetException?安卓2D游戏

android - (FRIDA) 生成失败 : unable to access zygote64 while preparing for app launch; try disabling Magisk Hide in case it is active