我需要使用自定义适配器实现具有自动完成功能的自定义搜索功能。我编写了自己的自定义适配器,代码如下:
的 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/icon_bg"
android:orientation="vertical"
tools:context="com.emc.kulkaa.myfinancials.MainActivity">
<AutoCompleteTextView
android:id="@+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_below="@+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="@drawable/round_border_edittext"
android:drawableEnd="@drawable/clear"
android:drawableLeft="@drawable/white"
android:drawableStart="@drawable/white"
android:ems="10"
android:hint="@string/search_hint"
android:singleLine="true"
android:textColor="@color/colorWhite"
android:textColorHint="@color/colorWhite" />
</LinearLayout>
custom_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/cardview_color">
<TextView
android:id="@+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
style="@style/simple"/>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name = "android:autoCompleteTextViewStyle">@style/simple</item>
</style>
<style name="simple" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:popupBackground">@drawable/simpleautocustom </item>
</style>
</resources>
simpleautocustom.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorWhite" />
<stroke
android:width="0dip"
android:color="@color/colorBlue" />
<corners android:radius="20dip" />
<padding
android:bottom="10dip"
android:left="25dip"
android:right="25dip"
android:top="10dip" />
</shape>
java code
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView mEdtSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
String arr[] = {"DSA LSA", "CHILD", "AICHILES", "CHILDREN", "FRANCE", "FRENCH"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, arr);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdtSearch = (AutoCompleteTextView) findViewById(R.id.edt_search);
mEdtSearch.setFocusable(true);
mEdtSearch.setFocusableInTouchMode(true);
mEdtSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mEdtSearch.setAdapter(adapter);
}
});
}
}
答案 0 :(得分:1)
我已经为您尝试了一些东西,希望它能解决您的问题,您可以根据需要自定义xmls,即custom_item.xml
TextView
的背景。
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9A000000"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_below="@+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="@drawable/round_border_edittext"
android:drawableEnd="@drawable/clear"
android:drawableLeft="@drawable/white"
android:drawableStart="@drawable/white"
android:ems="10"
android:hint="@string/search_hint"
android:popupBackground="@android:color/transparent"
android:singleLine="true"
android:textColor="@color/colorWhite"
android:textColorHint="@color/colorWhite"/>
</LinearLayout>
<强> custom_item.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<TextView
android:id="@+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/bg_rounded_border"
android:maxLines="1"
android:padding="15dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="15sp"/>
</RelativeLayout>
<强> bg_rounded_border.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/hint_color"/>
<corners android:radius="@dimen/padding_less"/>
</shape>
答案 1 :(得分:1)
您可以删除styles
。
只有您应将android:padding="7dp"
添加到父级LinearLayout,并将android:background="@drawable/simple_auto"
添加到custom_item
中的TextView。这是工作。
custom_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="7dp"
android:background="@color/cardview_color">
<TextView
android:id="@+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/simple_auto"
android:padding="10dp"
android:textSize="15sp"/>
</LinearLayout>