2014-05-02 30 views
0

我有3个VerticalViewPagers彼此相邻。每个视图分页器仅包含图像。这里的例子:Android:在VerticalViewPager中的ImageView上定位EditText

3 vertical view pagers

当用户点击图片,我需要一个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> 
+0

请发布您的Java和XML代码。 – rogcg

+0

编辑文本是否必须位于用户点击的位置或位于每个视图寻呼机的默认位置(如每个图像的底部)? – Parnit

+0

编辑文本必须位于每个图像的左上角,用户点击。 –

回答

1

还必须采取任务栏的高度成计算,因为getLocationOnScreen是位置时的布局是在任务栏的下面。在屏幕上定位对象时,应用程序布局的左上角为[0,0],但不是整个屏幕的[0,0]点,因为应用程序布局从任务栏下方开始。 getLocationOnScreen中的[0,0]点位于整个屏幕的左上角,因此您将获得稍微移动任务栏高度的坐标。 所以,当定位你的EditText时,只需将任务栏高度添加到y坐标,你应该很好。