【问题标题】:View.setX prevents adjustPan from workingView.setX 阻止 adjustPan 工作
【发布时间】:2019-05-08 17:37:28
【问题描述】:

我正在以编程方式设置 edittext 的 x 值。但是,这样做时,调整平移功能不起作用。关于如何完成这项工作的任何想法。 activity 未处于全屏模式,这只发生在 APIs > 23 中。

例如。

Edittext editext = findViewById(R.id.edittext);
edittext.setX(200);

//现在点击edittext,如果屏幕足够低就会被软键盘覆盖

注意:setY() 也会发生这种情况

这是一个重现错误的示例:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context context = this;
        setContentView(R.layout.activity_main);

        //Get edittext from layout file and set X and Y to the bottom
        //left of screen
        EditText edittext = findViewById(R.id.editText);
        edittext.setX(0);

        //Getting device frame to make sure its below keyboard, Not
        //necessary can just guess any value 
        edittext.setY(getDeviceFrame(context).getHeight() / 1.2f);
    }

布局为:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

还有一份清单:

<activity android:name=".MainActivity"
    android:windowSoftInputMode="adjustPan">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category 
                android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
</activity>

导致布局: Edittext after programmatic setX and setY

点击editext后,我们得到: Hidden editext

你能看到的被键盘隐藏了。

更新: 我尝试使用不同的变体来移动 EditText 仍然无济于事。

例如

edittext.animate().x(200);

更新 2: 我还没有解决这个问题。我已经越来越近了,因为我做了一些测试,似乎使用布局参数来控制视图的定位在一定程度上是有效的。

例如

ConstraintLayout.LayoutParams layoutParams = 
    (ConstraintLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = 200;  //Your X Coordinate
layoutParams.topMargin = 200;  //Your Y Coordinate
view.setLayoutParams(layoutParams);

在某种程度上,我的意思是我在测试库中弄乱了这些方法的功能,它似乎可以从 onCreate() 执行上述操作。但是,在我的应用程序中,我正在回调中进行调用。 SetX 工作正常,无需我在“post”线程或“runOnMainUI”线程中设置它。所以我不确定为什么这不起作用。

【问题讨论】:

  • 欢迎来到stackoverflow。如果您发布一个可重现的最小示例,您将获得更多有用的答案,以便其他用户可以重现您的错误。
  • @Simon.S.A.感谢您的建议,我添加了代码以帮助其他人重现错误

标签: android android-activity android-edittext adjustpan setx


【解决方案1】:

好的,所以我想出了如何解决这个问题。使用上面我的“更新 2”中的答案,我能够通过一些调整获得解决方案。

这段代码:

ConstraintLayout.LayoutParams layoutParams = 
    (ConstraintLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = 200;  //Your X Coordinate
layoutParams.topMargin = 200;  //Your Y Coordinate
view.setLayoutParams(layoutParams);

(你可以使用任何你正在使用的布局来代替约束)

是 setX() 和 setY() 的替代方法,允许 adjustPan 工作。但是,使用以下命令获取视图 x 或 y 定位:

view.getX();
view.getY();

不会返回视图的视觉定位。您需要通过获取与视图关联的布局参数并获取其边距来获取视图的位置。

现在我开始意识到约束布局的关键是您必须在 xml 中或通过代码定义约束。

例如

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

拥有这个允许边距引用它从什么延伸,没有这个,视图的定位不移动。

【讨论】:

    【解决方案2】:

    setXsetY 替换为leftMargintopMargin 用于弹出定位,它将在大于23 的API 上按预期工作。

    我无法解释原因,但 API 23 上不存在该问题。

    按照建议,这里是用于通过拖动按钮移动弹出布局并保持调整平移工作的代码示例。

            final View myNote = getActivity().getLayoutInflater().inflate(R.layout.viewer_annotation_layout, null);
            final RelativeLayout.LayoutParams noteLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    
            myNote.setLayoutParams(noteLayoutParams);
            mainScreenLayout.addView(myNote, noteLayoutParams);
    
    
            btnmove.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
    
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
    
                            //addednotedX = myNote.getX() - event.getRawX();
                            //addednotedY = myNote.getY() - event.getRawY();
    
                            addednotedX = noteLayoutParams.leftMargin - event.getRawX();
                            addednotedY = noteLayoutParams.topMargin - event.getRawY();
    
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int newposx = event.getRawX() + addednotedX;
                            int newposy = event.getRawY() + addednotedY;
    
                            /* this is move the Note layout but prevent adjsutpan working */
                            //myNote.setX(newposx);
                            //myNote.setY(newposy);
    
    
                            /* this is move the Note layout and keep adjsutpan working */
                            noteLayoutParams.leftMargin = newposx;
                            noteLayoutParams.topMargin =  newposy;
                            myNote.setLayoutParams(noteLayoutParams);
    
                            break;
                        default:
                            return false;
    
                    }
                    return false;
                }
            });
    

    【讨论】:

    • 能否提供一个sn-p代码供其他人日后参考?
    猜你喜欢
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2011-12-28
    • 2019-06-13
    相关资源
    最近更新 更多