【问题标题】:Android Studio SearchView UI extending at the right side of the screenAndroid Studio SearchView UI 在屏幕右侧扩展
【发布时间】:2016-12-08 11:03:09
【问题描述】:

我在 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" />

【问题讨论】:

  • 尝试将android:layout_width="343dp"更改为android:layout_width="match_parent"
  • @Char 你已经给定了边距,所以删除 343dp 并将 match_parent 设置为宽度

标签: android android-studio searchview android-search


【解决方案1】:

尽量不要硬编码。因此,删除 343dp 宽度值并将其替换为 ma​​tch_parent

android:layout_width="match_parent"

如果父级是RelativeLayout,则使用它来居中视图:

android:layout_centerHorizontal="true"

并设置一个整体边距以使其远离边框。

【讨论】:

    【解决方案2】:

    使用重力和边距来解决您的问题

    android:gravity="center_horizontal"
    android:layout_margin="5dp"
    

    【讨论】:

      【解决方案3】:

      尽量避免使用硬编码的“343dp”,因为每台设备都有数百种屏幕尺寸,如果你在平板电脑上使用“343dp”,你的搜索视图会变小,如果你是在小屏幕手机上,你的searchview 会变长。 将您的代码更改为此

      <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" />
      

      【讨论】:

        猜你喜欢
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 2020-02-07
        • 1970-01-01
        • 2019-01-20
        • 1970-01-01
        • 2012-01-07
        相关资源
        最近更新 更多