我在工具栏中设置了autocompletetextview
。当用户提供搜索查询时,我有一个适配器(添加了一些虚拟数据用于测试),根据搜索查询显示结果。
问题在于适配器宽度,因为它设置为autocompletetextview
宽度。
但在这里我希望适配器宽度设置为工具栏宽度和工具栏与适配器之间的边距。
这是我写的代码:
searchView = (AutoCompleteTextView) findViewById(R.id.searchView);
SearchAdapter searchAdapter = new SearchAdapter(NavigationDrawerActivity.this,searchResults,searchView);
searchView.setAdapter(searchAdapter);
这是我的适配器:
public class SearchAdapter extends ArrayAdapter<SearchResult> {
private boolean mAnimate;
private AutoCompleteTextView mSearch;
public SearchAdapter(Context context, List<SearchResult> options, AutoCompleteTextView search) {
super(context, 0, options);
mSearch = search;
}
int count = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchResult option = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.search_option, parent, false);
/* if (true) {
Animation anim = AnimationUtils.loadAnimation(getContext(),
R.anim.anim_down);
anim.setDuration(400);
convertView.startAnimation(anim);
if (count == this.getCount()) {
mAnimate = false;
}
count++;
}*/
}
View border = convertView.findViewById(R.id.border);
if (position == 0) {
border.setVisibility(View.VISIBLE);
} else {
border.setVisibility(View.GONE);
}
final TextView title = (TextView) convertView
.findViewById(R.id.title);
title.setText(option.title);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
icon.setImageDrawable(option.icon);
ImageView up = (ImageView) convertView.findViewById(R.id.up);
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSearch.setText(title.getText().toString());
mSearch.setSelection(mSearch.getText().length());
}
});
return convertView;
}
}
这是我的适配器行布局:
<?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="48dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:paddingLeft="3dp"
android:adjustViewBounds="true"
android:cropToPadding="true" />
<TextView
android:id="@+id/title"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#212121"
android:layout_centerVertical="true"
android:layout_marginLeft="62dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/up"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/up"
android:layout_alignParentRight="true"
android:layout_width="32dp"
android:layout_margin="10dp"
android:layout_marginRight="24dp"
android:layout_gravity="center|right"
android:layout_height="32dp"
android:src="@drawable/ic_up" />
<View
android:id="@+id/border"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E8E8" />
</RelativeLayout>
使用上面的代码我会这样: Need the adapter aligned to toolbar width 我试过像这样设置适配器宽度:
Rect screenSize = new Rect();
getWindowManager().getDefaultDisplay().getRectSize(screenSize);
// screen width
int screenWidth = screenSize.width();
int adapterMargin = (int)getResources().getDimension(R.dimen.adapter_margin);
searchView.setDropDownWidth(screenWidth - adapterMargin);
但问题是我没有获得正确的保证金,如下所示: screen with adapter width set
请帮我解决这个问题。
答案 0 :(得分:4)
看起来您想要将适配器宽度设置为工具栏宽度,将以下行添加到您的代码中它将起作用:
searchView.setDropDownAnchor(toolbar.getId());
如果您有任何疑问,请在下方发表评论
答案 1 :(得分:0)
修改此行
searchView.setDropDownWidth(screenWidth - adapterMargin);
以下,可能会改变。
searchView.setDropDownWidth(screenWidth - adapterMargin * 2);