我有3个VerticalViewPagers彼此相邻。每个视图分页器仅包含图像。这里的例子:Android:在VerticalViewPager中的ImageView上定位EditText
当用户点击图片,我需要一个EditText。 EditText必须位于每个图像的确切位置。所以我需要获取图像的位置,添加一些常量并创建一个EditText。这个怎么做?
注意:我试过getLocationOnScreen,对于最左边的图像,我得到了[0,50],当我为EditText设置margin top = 50和left = 0时,它位于图像上方。
充气物品:
public Object instantiateItem(ViewGroup collection, final int position) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rootView = layoutInflater.inflate(R.layout.item_layout, null);
final ImageView imageView = (ImageView) rootView.findViewById(R.id.item);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
collection.addView(rootView, 0);
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int[] pos = new int[2];
imageView.getLocationOnScreen(pos);
params.leftMargin = coordinates.get(0).first;
params.topMargin = coordinates.get(0).second;
EditText editText = new EditText(context);
editText.setLayoutParams(params);
((RelativeLayout) rootView).addView(editText);
}
}
});
return rootView;
item_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_relative_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/item"
android:contentDescription="@string/imageContent" />
</RelativeLayout>
请发布您的Java和XML代码。 – rogcg
编辑文本是否必须位于用户点击的位置或位于每个视图寻呼机的默认位置(如每个图像的底部)? – Parnit
编辑文本必须位于每个图像的左上角,用户点击。 –