gpt4 book ai didi

android - Android LinearLayout中TextView的拖动实现方法

转载 作者:行者123 更新时间:2023-11-29 01:54:50 27 4
gpt4 key购买 nike

下面是我的layout.xml:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="30dp" >

<LinearLayout
android:id="@+id/ll1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Medium Text"
android:background="#000000"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

</LinearLayout>

我想让 TeextView tv1 只能在 LinearLayout ll1 中水平拖动。
我实现如下:

ll1 = (LinearLayout)findViewById(R.id.ll1);
tv1 = (TextView)findViewById(R.id.tv1);

Rect rectf = new Rect();
ll1.getGlobalVisibleRect(rectf);
LayoutLeft = rectf.left;

tv1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float TouchX = event.getX();
System.out.println("TouchX " + TouchX);
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
if(TouchX < LayoutLeft) {
ll1.setPadding(0, 0, 0, 0);
}
else if(TouchX > (LayoutLeft + 100)) {
ll1.setPadding(100, 0, 0, 0);
}
else {
ll1.setPadding((int)(TouchX - LayoutLeft), 0, 0, 0);
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
}
return true;
}
});

但是我测试从左到右拖动,我得到的 TouchX 值如下:

04-03 14:24:18.061: I/System.out(18468): TouchX 43.554024
04-03 14:24:18.631: I/System.out(18468): TouchX 92.101845
04-03 14:24:19.731: I/System.out(18468): TouchX 49.150528
04-03 14:24:20.371: I/System.out(18468): TouchX 100.82299
04-03 14:24:22.311: I/System.out(18468): TouchX 54.747032
04-03 14:24:22.781: I/System.out(18468): TouchX 74.458405
04-03 14:24:22.801: I/System.out(18468): TouchX 6.1168213
04-03 14:24:22.821: I/System.out(18468): TouchX 80.44317
04-03 14:24:22.851: I/System.out(18468): TouchX 21.571915
04-03 14:24:22.881: I/System.out(18468): TouchX 89.24452

为什么 TouchX 值不符合顺序?
我该如何修改它?

最佳答案

ll1 是一个线性布局,我猜你的布局中有你的 TextView 。如果您将填充设置为您的线性布局,那么 View 中的子项也将被添加填充。您必须为线性布局或 TextView 设置触摸事件。您还可以更改边距而不是 TextView 。为您的问题找到我的解决方案

ll1 = (LinearLayout)findViewById(R.id.ll1);
tv1 = (TextView)findViewById(R.id.tv1);

LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.fill_parent,
height * 2);
/or whatever you want to be margins for
layout_param.setMargins(30, 20, 30, 0);


Rect rectf = new Rect();
ll1.getGlobalVisibleRect(rectf);
LayoutLeft = rectf.left;

ll1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float TouchX = event.getX();
System.out.println("TouchX " + TouchX);
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
//ALTER your layoutparams as per your need.
//do calculations on your layout param
if(TouchX < LayoutLeft) {
ll1.setLayoutParams(layout_param);
}
else if(TouchX > (LayoutLeft + 100)) {
ll1.setLayoutParams(layout_param);
}
else {
ll1.setLayoutParams(layout_param);
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
}
return true;
}
});

希望这能解决您的问题。

关于android - Android LinearLayout中TextView的拖动实现方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15780304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com