我试图删除 Android 搜索 View 之前的左侧空间,但没有成功。我尝试了在 Internet 上找到的所有答案,但没有用。 我正在为此 Activity 使用 AppCompat 主题。我的目标是 KitKat api (19)。
编辑
Activity 布局
<?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="match_parent"
android:id="@+id/MainLayout"
android:background="#6ED19E">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/BackgroundLayout"
android:background="#6ED19E">
<ImageView
android:src="@drawable/bgpattern"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/PatternImageView"
android:scaleType="centerCrop" />
<TextView
android:text="Já estamos em muitas cidades\nEstamos na sua?"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ManyCityLabel"
android:textColor="#ffffff"
android:textSize="25dp"
android:width="20dp"
android:gravity="center"
android:textStyle="normal" />
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LocationsListView"
android:background="#ffffff"
android:visibility="invisible" />
菜单
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<item android:id="@+id/LocationSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:showAsAction="always"
tools:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
我以编程方式执行的图标化操作是因为当我更改 xml 时不起作用
编辑 如果我设置 SearchView 的背景颜色
搜索不适合整个空间。 为什么?
最佳答案
您可以执行以下操作:
在 onCreate()
中隐藏应用栏,如下所示:
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
在布局文件中将背景设置为绿色(或您的颜色),添加一个 SearchView
并将 SearchView
的背景设置为白色,如下所示:
<android.support.constraint.ConstraintLayout
...
android:background="#00FF00"
...>
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF" />
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
....
</android.support.constraint.ConstraintLayout>
我很确定您的其他搜索界面代码应该可以工作。您只需要重定向您的事件调用。
关于android - 如何删除Android SearchView 的剩余空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071396/