我的SearchView显示如下:
正如您所看到search_edit_frame的,SearchView和SearchView本身之外都有一个边距.
这是从哪里来的?检查布局显示左边24像素的边距, search_edit_frame但其他地方没有边距.
如何删除这个额外的空间?
menu.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/appbar_menu_search"
android:icon="@drawable/ic_search"
android:title="@string/search_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Run Code Online (Sandbox Code Playgroud)
布局:
<RelativeLayout
android:id="@+id/root_layout"
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">
<android.support.design.widget.AppBarLayout
android:id="@+id/profile_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar_gradient"
android:theme="@style/AppTheme.Dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar_gradient"
android:fitsSystemWindows="true"
android:minHeight="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
(...)
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我设法通过应用@Jimmy的答案来缩小间距:
然而,"后"图像按钮和SearchView之间仍然存在很大差距.
search_edit_frame是一个LinearLayout布局searchview。默认情况下,它的开始和结束边距为 8dip。
源码中的相关代码
<LinearLayout
android:id="@+id/search_edit_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="8dip"
android:layout_marginEnd="8dip"
android:orientation="horizontal"
android:layoutDirection="locale">
Run Code Online (Sandbox Code Playgroud)
您的问题可能是因为它的起始保证金。您可以从搜索视图中获取此线性布局,并设置布局参数以减少活动中的间隙(可能onCreateOptionsMenu在您要膨胀的位置的内部或类似位置searchview)。
像这样的东西,假设searchView是你的 SearchView 实例,
LinearLayout searchFrameLL=(LinearLayout)searchView.findViewById(R.id.search_edit_frame);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(0,0,8,0); //params.setMargins(left, top, right, bottom)
// params.setMarginStart(0); //(or just use individual like this
searchFrameLL.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
以类似的方式,您可以更新该 xml 中的其他属性以满足您的需求。
| 归档时间: |
|
| 查看次数: |
846 次 |
| 最近记录: |