Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何适应EditText';s RightView(根据编辑文本高度)_Android - Fatal编程技术网

Android:如何适应EditText';s RightView(根据编辑文本高度)

Android:如何适应EditText';s RightView(根据编辑文本高度),android,Android,我需要将图标(搜索图标)设置为EditText的RightView,我使用了以下xml: android:drawableRight="@drawable/search_icon" 问题是RightView不适合EditText,而是EditText的高度会根据图标高度进行更改。。是否可以执行相反的操作,根据EditText的高度编辑RightView的高度 编辑文本的XML: <EditText android:id="@+id/regSearc

我需要将图标(搜索图标)设置为EditText的
RightView
,我使用了以下xml:

android:drawableRight="@drawable/search_icon"
问题是RightView不适合EditText,而是EditText的高度会根据图标高度进行更改。。是否可以执行相反的操作,根据EditText的高度编辑RightView的高度

编辑文本的XML:

<EditText
                    android:id="@+id/regSearch"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="18dp"
                    android:layout_marginLeft="45dp"
                    android:layout_marginRight="45dp"
                    android:ems="10"
                    android:hint="Search Test"
                    android:inputType="phone"
                    android:padding="5dip"
                    android:textColor="@color/lowBlack"
                    android:textSize="16sp"
                    android:drawableRight="@drawable/search_icon" />

不使用drawableRight,您可以稍微调整一下布局,在edittext旁边有一个imageview,然后可以播放两个视图的高度和宽度。e、 g.这可以使用RelativeLayout和edittext和imageview在以下范围内实现:

<RelativeLayout>
  <EditText android:id="my_edittext"/>
  <ImageView android:toRightOf="@+id/my_edittext"
    android:layout_alignTop="@+id/my_edittext"
    android:layout_alignBottom="@+id/my_edittext"/>
<RelativeLayout/>

alignTop和alignBottom属性将处理您面临的高度问题。toRightOf将把imageview放在布局中编辑文本的右侧

但是,如果希望imgeview覆盖edittext,也可以使用此布局实现这一点-只需删除“toRightOf”属性并将alignParentRight=“true”添加到ImageView


注意:我还没有在这里编写完整的xml代码。如果您需要更多帮助,请告诉我。

最后,在搜索了一段时间后,我提出了以下解决方案,不需要创建任何其他“黑客”元素:


通过这种方式,您可以设置和编辑所需的任何可绘制图形的尺寸,希望它能帮助其他人,因为我在这个问题上陷入了一段时间:P

提供您的textview代码please@vishaljangid添加了,但我很确定XML无法实现我想要的功能。我认为您应该使用自定义编辑文本,或者根据需要创建搜索图标图像大小,但我不想按顺序使用其他元素为了达到这个效果,我已经在上面发布了对我有效的解决方案,无论如何,谢谢!)
regSearch.getViewTreeObserver()
        .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                Drawable img = Registrazione.this.getResources().getDrawable(
                R.drawable.search_icon);
                img.setBounds(0, 0, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3); //you can edit this in order to resize the drawable
                regSearch.setCompoundDrawables(null, null, img, null);
                regSearch.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });