如何使的LinearLayout TextView的拖累流畅,在Android的? [英] How to make the TextView drag in LinearLayout smooth, in android?

查看:137
本文介绍了如何使的LinearLayout TextView的拖累流畅,在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况,我不能够解决,希望我会得到一些建议你。

I have a situation which I am not able to fix, hopefully I will get some advises from you.

这种情况很简单:我有一个的LinearLayout中,我有一个多行文本的一个TextView。用户能够直到他找到他喜欢的位置上的TextView拖动左右。什么是真正重要的是,TextView的可部分出的LinearLayout的(它会出现下调)。

The situation is simple: I have a LinearLayout in which I have a TextView with multiple lines of text. The user is able to drag the TextView around until he finds a position he likes. What is really important is that the TextView can be partially out of the LinearLayout (it will appear cut).

下面是一些code样品:<​​/ P>

Here are some code samples:

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:clipChildren="false"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/text_color"
            android:textSize="16sp" />
    </LinearLayout>

正如你所看到的LinearLayout有clipChildren =假,让文字截止。 对于TextView中我设置一个触摸监听器

As you can see the LinearLayout has clipChildren = false to allow cutoff of text. For the textview I set a touch listener

txt.setOnTouchListener(new View.OnTouchListener() {
            int initialX = 0;
            int initialY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = (int) event.getX();
                    initialY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                            int currentX = (int) event.getX();
                            int currentY = (int) event.getY();
                            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) txt.getLayoutParams();

                            int left = lp.leftMargin + (currentX - initialX);
                            int top = lp.topMargin + (currentY - initialY);
                            int right = lp.rightMargin - (currentX - initialX);
                            int bottom = lp.bottomMargin - (currentY - initialY);

                            lp.rightMargin = right;
                            lp.leftMargin = left;
                            lp.bottomMargin = bottom;
                            lp.topMargin = top;

                            txt.setLayoutParams(lp);
                    break;
                default:
                    break;
                }
                return true;
            }
        });

这里是我的问题:正如你看到的,我已经将所有的布局参数(右,左,下,上边距)。这是为什么呢?

And here is my problem: As you can see I've set all the layout parameters (right, left, bottom, top margins). Why is that ?

1)如果我只用左/上方的阻力是足够多的,但流畅的文字获取的包裹在适当的边界,而不是获得晋级。可能是由于右0边距值。

1) If I use only left/top the drag is smooth enought but text get's wrapped at the right border instead of getting cut. Probably due to right 0 margin value.

2)如果我使用所有的利润,文本被切断,因为我想要的,但运动不顺畅,它只是跳绕了几个像素。

2) If I use all margins, the text gets cut as I want, but the movement is not smooth, it just jumps around for a few pixels.

我该如何解决这个问题?

How can I fix this ?

推荐答案

嘛。这是奇怪的。通过插入的TextView内的另一个的LinearLayout具有相同大小作为初始一体,使一切顺利。我不知道为什么。

Well. this is weird. By inserting the TextView inside another LinearLayout with the same size as the initial one, makes everything smooth. I have no idea why.

这篇关于如何使的LinearLayout TextView的拖累流畅,在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆