【问题标题】:Android TextView with Marquee and layout_weight not scrolling带有 Marquee 和 layout_weight 的 Android TextView 不滚动
【发布时间】:2017-09-22 08:18:55
【问题描述】:

我弹出了一个带有标题和关闭按钮的对话框。这是 XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:maxLines="1"
        <!--- android:singleLine="true" <-- Do not comment about this, it is deprecated --->
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        />

    <ImageView
        android:id="@+id/backburger"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_close"
        android:foreground="?android:attr/selectableItemBackground"
        />

</LinearLayout>

就像您可以看到 TextView 具有选取框所需的所有东西一样,singleLine 选项已被弃用,因此我无法使用它。我确实在我的 Java 中调用了setSelected(true)。有很多关于选取框的问题,但对于 layout_weight 和 17 年的问题都没有。

这是 XML 的预览:

【问题讨论】:

标签: java android xml


【解决方案1】:

试试这个: 在您的 XML 中:

<TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
         android:textSize="20sp"
        android:textColor="@android:color/black" />

和你的代码:

tv = (TextView) this.findViewById(R.id.text_title);  
tv.setSelected(true);

希望它的帮助:)

【讨论】:

  • @Finiox 您的问题令人困惑。我无法理解你的要求。请清楚说明您的要求。你想要什么输出?
  • 添加此行后尝试 tv.requestFocus();
【解决方案2】:
public void setticker(String text, TextView view) {
    if (text != "") {


        view.setText(text);
        //view.setTextSize(25.0F);
        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 width of the view
        float width = view.getMeasuredWidth();
        float height = view.getMeasuredHeight();

        // gets the screen width
        float screenWidth = ((Activity) context).getWindowManager()
                .getDefaultDisplay().getWidth();

        // view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
        //     (int) height, 1f));

        System.out.println("width and screenwidth are" + width + "/"
                + screenWidth + "///" + view.getMeasuredWidth());

        // performs the calculation
        float toXDelta = width - (screenWidth - 0);

        // sets toXDelta to -300 if the text width is smaller that the
        // screen size
        if (toXDelta < 0) {
            toXDelta = 0 - screenWidth;// -300;
        } else {
            toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
        }
        // Animation parameters
        Animation mAnimation = new TranslateAnimation(screenWidth,
                toXDelta, 0, 0);
        mAnimation.setDuration(15000);
        mAnimation.setRepeatMode(Animation.RESTART);
        mAnimation.setRepeatCount(Animation.INFINITE);
        view.setAnimation(mAnimation);
        //parent_layout.addView(view);
    }
}

【讨论】:

  • 所以这将用自定义动画师替换选取框?只有一个问题,我有一个MaterialDialog 将在其中TextView,这意味着您的float screenWidth 将太宽。
  • 是的,它会太宽。而不是屏幕宽度尝试用对话框宽度计算它
猜你喜欢
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多