SearchView宽度+自动完成下拉的边距

时间:2015-09-01 13:55:33

标签: android autocompletetextview searchview

我尝试将<html> <head> <title>Countdown</title> <script type="text/javascript"> var seconds = 10; function stopTimer() { clearTimeout(countdownTimer) } function floor(x) { return x | 0; } function pad(n) { if (n < 0) { n = -n; } if (n < 10) { return '0' + n.toString(); } return n.toString(); } function secondPassed() { var minutes = pad(floor(seconds / 60)); if (seconds < 0) { minutes = '-' + minutes; } var remainingSeconds = pad(seconds % 60); document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds; if (seconds > 0) { seconds--; if (seconds > 8) { document.body.style.backgroundColor = "green"; } else if (seconds == 5) { document.body.style.backgroundColor = "yellow"; } else if (minutes == 0 & seconds == 0) { document.body.style.backgroundColor = "red"; if (seconds%2 == 0) { document.getElementById('skull').style.display="none"; } if (seconds%2 != 0) { document.getElementById('skull').style.display="block"; } } } else { if (seconds%2 == 0) { document.getElementById('skull').style.display="block"; } if (seconds%2 != 0) { document.getElementById('skull').style.display="none"; } seconds--; } } </script> </head> <body> <img id="skull" src="http://s15.postimg.org/es5w3xpob/skull.gif"style="position:absolute; z-index: -1;display:none;"> <div style=" z-index:10;"> <p align ="center"> <span id="countdown" style="color:black; font-size: 450px; font-weight: bold;" ></span> </br> <button onclick="countdownTimer = setInterval('secondPassed()', 1000)">Start</button> <button onclick="stopTimer()">Stop</button> </p> </div> </body> </html> 生成的下拉列表ListView设置为AutoCompleteTextView(屏幕的全宽)

这是我到目前为止所得到的: enter image description here

我还使用match_parent启发了这个答案Here

以下是膨胀菜单的活动

SearchView

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); MenuItem searchItem = menu.findItem(R.id.action_search); searchItem.expandActionView(); MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { return false; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { onBackPressed(); return false; } }); final ArrayAdapterSearchView searchView = (ArrayAdapterSearchView) MenuItemCompat.getActionView(searchItem); searchView.setQueryHint(getResources().getString(R.string.location_autocomplete_hint)); searchView.setAdapter(mPlaceAdapter); searchView.setDropDownAnchor(R.id.anchor_dropdown); searchView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { searchView.setText(mPlaceAdapter.getItem(position).toString()); startSearch(position); } });

ArrayAdapter

最后是public class ArrayAdapterSearchView extends SearchView { private SearchView.SearchAutoComplete mSearchAutoComplete; public ArrayAdapterSearchView(Context context) { super(context); initialize(); } public ArrayAdapterSearchView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } public void initialize() { mSearchAutoComplete = (SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text); this.setAdapter(null); this.setOnItemClickListener(null); } @Override public void setSuggestionsAdapter(CursorAdapter adapter) { // don't let anyone touch this } public void setOnItemClickListener(AdapterView.OnItemClickListener listener) { mSearchAutoComplete.setOnItemClickListener(listener); } public void setAdapter(ArrayAdapter<?> adapter) { mSearchAutoComplete.setAdapter(adapter); } public void setText(String text) { mSearchAutoComplete.setText(text); } public void setDropDownVerticalOffset(int value) { mSearchAutoComplete.setDropDownVerticalOffset(value); } public void setDropDownAnchor(int value) { mSearchAutoComplete.setDropDownAnchor(value); } public void setDropDownWidth (int value) { mSearchAutoComplete.setDropDownWidth(value); } public void setDropDownBackgroundResource(Drawable value) { mSearchAutoComplete.setDropDownBackgroundDrawable(value); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mSearchAutoComplete.getLayoutParams(); params.setMargins(0, 0, 0, 0); //substitute parameters for left, top, right, bottom mSearchAutoComplete.setLayoutParams(params); } } 文件

menu_search.xml

主要活动xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_margin="0dp"
    android:padding="0dp"
    tools:context="com.example.activities.SearchActivity">

    <item
        android:id="@+id/action_search"
        android:hint="@string/location_autocomplete_hint"
        android:title="@string/location_autocomplete_hint"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView"
        app:actionViewClass="com.example.adapters.ArrayAdapterSearchView" />

</menu>

最后,我使用简单的TextView填充ListView。

我的想法

我认为SearchView与此问题有关,因为如果我用简单的AutoCompleteTextView替换SearchView,下拉列表将占据屏幕的整个宽度。

谢谢!

1 个答案:

答案 0 :(得分:0)

我的方法是创建另一个左右边距的锚视图,高度为1dp。 setDropdownAnchor()到该视图,下拉列表将根据您的需要匹配您的视图