2014-05-02 24 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座標,你應該很好。