繁体   English   中英

Android:在VerticalViewPager的ImageView上放置EditText

[英]Android: positioning EditText on ImageView in VerticalViewPager

我彼此之间有3 VerticalViewPagers。 每个视图寻呼机仅包含图像。 例子如下:

3个垂直视图传呼机

当用户单击图像时,我需要制作一个EditText。 每个图像的EditText必须在确切位置。 因此,我需要获取图像的位置,添加一些常量,然后在其中添加一个EditText。 这个怎么做?

注意:我尝试了getLocationOnScreen,对于最左边的图像,我得到了[0,50],当我将margintop设置为50且EditText的左边设置为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>

您还必须考虑任务栏的高度,因为getLocationOnScreen是布局在任务栏下方时的位置。 在屏幕上放置对象时,应用程序布局的左上角为[0,0],但不是整个屏幕的[0,0]点,因为应用程序布局从任务栏下方开始。 getLocationOnScreen中的[0,0]点位于整个屏幕的左上角,因此您可以通过任务栏高度稍微移动坐标。 因此,在放置EditText时,只需将任务栏的高度添加到y坐标,就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM