简体   繁体   English

带有字幕和layout_weight的Android TextView不滚动

[英]Android TextView with Marquee and layout_weight not scrolling

I have a dialog popping up with a title and a close button. 我弹出一个带有标题和关闭按钮的对话框。 This is the XML: 这是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>

Like you can see the TextView has all the things it needs for a marquee, the singleLine options is deprecated, so I can't use it. 就像您可以看到TextView具有选取框所需的所有功能一样, singleLine选项已弃用,因此我无法使用它。 I do call setSelected(true) in my Java. 我确实在Java中调用setSelected(true) There are lots of questions about a marquee, but none with layout_weight and from '17. 关于选框有很多问题,但是没有关于layout_weight和'17以来的问题。

This is the preview of the XML: 这是XML的预览:

产量

Try this one: In your Xml: 试试这个:在您的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" />

and your Code: 和您的代码:

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

Hope its help:) 希望它的帮助:)

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM