我有一个带有物品的listview
和我的searchview
中有一个toolbar
。当用户单击搜索图标时,将出现键盘。有一个问题使我发疯。当键盘出现时,用户无法滚动到最后四个项目,是的,我尝试了android:windowSoftInputMode="adjustResize"
和android:windowSoftInputMode="adjustPan"
,但它们什么也没做。
清单:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Adapter">
</activity>
<activity
android:name=".OnSwipeTouchListener"
android:label="@string/title_activity_on_swipe_touch_listener"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Activity2"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity.xml
<androidx.constraintlayout.widget.ConstraintLayout 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="To get started please click the button below !"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="@+id/mainlistview"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="@+id/mainlistview"
app:layout_constraintTop_toTopOf="@+id/mainlistview"
app:layout_constraintVertical_bias="0.626" />
<Button
android:id="@+id/button"
android:layout_width="123dp"
android:layout_height="59dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Open m3u"
app:layout_constraintBottom_toBottomOf="@+id/mainlistview"
app:layout_constraintEnd_toEndOf="@+id/mainlistview"
app:layout_constraintStart_toStartOf="@+id/mainlistview"
app:layout_constraintTop_toTopOf="@+id/mainlistview" />
<ListView
android:id="@+id/mainlistview"
android:layout_width="match_parent"
android:layout_height="673dp"
android:layout_marginTop="2dp"
android:gravity="right"
android:textSize="2dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
tools:layout_editor_absoluteX="-144dp">
</ListView><![CDATA[
/>/>
]]>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:0)
尝试一下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
res / menu / toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu>
<item
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="@string/search"/>
</menu>
注意:我使用android:entries属性填充了仅16个项目的列表 具有字符串数组的值(在下面的星号内)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:entries="@array/dummy_items"**
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
/>
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/Theme.AppCompat" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
和 styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- whatever customization -->
build.gradle (应用程序之一)
...
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
}
我建议实施SearchDialog
活动。
它显示了一个弹出对话框,用于提示建议,并且是一个对话框,没有出现在键盘后面。
阅读此官方Android指南以了解更多信息。 https://developer.android.com/guide/topics/search/search-dialog#SearchDialog