【问题标题】:android: button not centered when turned from portrait to landscape modeandroid:从纵向模式变为横向模式时按钮不居中
【发布时间】:2017-08-01 05:49:16
【问题描述】:

我有一个具有以下设置的相对布局中的 ImageButton:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000032">
    <ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:contentDescription="button"
    android:scaleType="fitCenter"
    android:src="@drawable/buttonImage" />
</RelativeLayout>

它在 layout 和 layout-land 文件中。

当应用以横向模式启动时,按钮在横向模式下正确居中,在将设备转为纵向模式时保持居中。

当应用程序以纵向模式启动时,按钮在纵向模式下正确居中,但在将设备切换到横向模式时不再居中。不知道为什么?

【问题讨论】:

  • 发布您的完整 XML 布局和布局域
  • @EricBachhuber 它们和上面显示的一样。
  • 顺便说一句,android:orientation 对于RelativeLayout 来说是一个空操作。此外,您可以尝试将这两行(android:layoutCenterHorizo​​ntal="true" 和 android:layoutCenterVertical="true")替换为以下行:android:layout_centerInParent="true"
  • @MichaelKrause 试过了,似乎没有解决我的问题。

标签: android


【解决方案1】:

添加

android:configChanges="orientation"

在您的androidmanifest.xml 文件中使用的活动中
它应该可以工作
https://developer.android.com/guide/topics/manifest/activity-element.html#config

【讨论】:

  • 我已经有了这个:android:configChanges="orientation|screenSize|fontScale"
  • 如果您使用 API 级别 13 或更高级别,那么您还应该声明“screenSize”配置
  • 我已经在我的手机和工作上试了试。检查是否有任何干扰
  • 我想我错过了普通布局文件中的 android:layout_centerHorizo​​ntal="true" ......它现在正在工作
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
相关资源
最近更新 更多