Android Studio SearchView UI 在屏幕右侧扩展
Android Studio SearchView UI extending at the right side of the screen
我在 Android Studio 预览中的 SearchView 如下所示:
https://i.stack.imgur.com/Khyy5.png
但在我的物理设备中,它看起来像这样:
https://i.stack.imgur.com/PlrFX.jpg
我该如何解决这个问题?让它不要在右侧扩展。我的 xml
中只有这个代码
<SearchView
android:layout_width="343dp"
android:layout_height="wrap_content"
android:background="#fafafa"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
使用Gravity和margin来解决你的问题
android:gravity="center_horizontal"
android:layout_margin="5dp"
尽量不要硬编码。因此,删除 343dp
宽度值并将其替换为 match_parent.
android:layout_width="match_parent"
如果父级是 RelativeLayout
则使用它来居中视图:
android:layout_centerHorizontal="true"
并设置总边距以使其远离边框。
尽可能避免使用硬编码的“343dp”,因为每个设备上都有数百种屏幕尺寸,如果您在平板电脑上使用“343dp”,您的搜索视图会变小,如果您是在小屏幕手机上使用,您的搜索视图将变得更长。
将您的代码更改为此
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
我在 Android Studio 预览中的 SearchView 如下所示:
https://i.stack.imgur.com/Khyy5.png
但在我的物理设备中,它看起来像这样:
https://i.stack.imgur.com/PlrFX.jpg
我该如何解决这个问题?让它不要在右侧扩展。我的 xml
中只有这个代码<SearchView
android:layout_width="343dp"
android:layout_height="wrap_content"
android:background="#fafafa"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
使用Gravity和margin来解决你的问题
android:gravity="center_horizontal"
android:layout_margin="5dp"
尽量不要硬编码。因此,删除 343dp
宽度值并将其替换为 match_parent.
android:layout_width="match_parent"
如果父级是 RelativeLayout
则使用它来居中视图:
android:layout_centerHorizontal="true"
并设置总边距以使其远离边框。
尽可能避免使用硬编码的“343dp”,因为每个设备上都有数百种屏幕尺寸,如果您在平板电脑上使用“343dp”,您的搜索视图会变小,如果您是在小屏幕手机上使用,您的搜索视图将变得更长。 将您的代码更改为此
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />