我有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
編輯文本必須位於每個圖像的左上角,用戶點擊。 –