我正在以编程方式设置edittext的x值。但是,在这样做时,调整pan功能不起作用。关于如何使这件事奏效的任何想法。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>导致布局:编程setX和setY之后的编辑
点击编辑后,我们得到:隐藏编辑
你可以看到它隐藏在键盘上。
更新:我尝试使用不同的变体来移动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’线程。所以我不知道为什么这不起作用。
发布于 2019-02-04 21:15:13
好吧,所以我想出了解决问题的办法。使用上面我的“更新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"有了这个,边距就可以引用它是从什么扩展出来的,没有这个,视图的位置就没有移动。
发布于 2020-04-08 18:18:53
将setX和setY替换为leftMargin和topMargin以进行弹出定位,并且它将按照预期在API大于23的情况下工作。
我无法解释为什么,但是这个问题在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;
}
});https://stackoverflow.com/questions/53661302
复制相似问题