带有Marquee和layout_weight的Android TextView不滚动

时间:2017-09-22 08:18:56

标签: java android 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>

就像您可以看到TextView具有选取框所需的所有内容一样,singleLine选项已弃用,因此我无法使用它。我在Java中调用setSelected(true)。有很多关于字幕的问题,但没有关于layout_weight和“17”的问题。

这是XML的预览:

Output

2 个答案:

答案 0 :(得分: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);

希望它的帮助:)

答案 1 :(得分:0)

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