我想扩展 AutoCompleteTextView 的 UI。功能很好,我只需要在右侧添加一个看起来像下拉按钮的按钮。可悲的是 AutoCompleteTextView 有一个“自然”的边距,我不能减少到 0。
我现在能做什么?我必须覆盖 onDraw() 和 onMeasure() 来归档我的目标(有没有更简单的方法)?
我想扩展 AutoCompleteTextView 的 UI。功能很好,我只需要在右侧添加一个看起来像下拉按钮的按钮。可悲的是 AutoCompleteTextView 有一个“自然”的边距,我不能减少到 0。
我现在能做什么?我必须覆盖 onDraw() 和 onMeasure() 来归档我的目标(有没有更简单的方法)?
您可以使用RelativeLayout
将 Button 放在AutoCompleteTextView
样本
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_close_pressed"
android:layout_alignParentRight="true"
android:id="@+id/myBtn"
></Button>
<AutoCompleteTextView android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_toLeftOf="@+id/myBtn"
/>
</RelativeLayout>
您可以将AutoCompleteTextView
和 按钮都放在 上FrameLayout
,添加一些额外的边距AutoCompleteTextView
以使其FrameLayout
稍大,然后将按钮与父级对齐。事实上,这两个视图会相互干扰,但对于用户来说,它们会一个挨一个出现,没有任何边距。
另一种选择可能是将自定义背景设置为AutoCompleteTextView
(可能修改了从 Android 源获取的原始背景,并删除了边距)。
只记得您可以提供负保证金。例如,您可以将两个视图放在LinearLayout
按钮的左边距上并将其设置-5dp
为。但是,您仍然必须为按钮提供自定义无边距背景。