【问题标题】:android Imageview hides elements below when changing orientationandroid Imageview在更改方向时隐藏下面的元素
【发布时间】:2023-03-18 18:52:01
【问题描述】:

我在更改屏幕方向时遇到问题。当手机处于纵向模式时,一切正常,但是当我将方向更改为横向模式时,imageview 隐藏了下面的按钮。

这是我的布局 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:longClickable="false">


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Your Inbox"
    android:id="@+id/btn_inbox"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_below="@+id/btn_inbox"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/img_main" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Scan QR"
    android:id="@+id/btn_qrscan"
    android:layout_below="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:enabled="false" />

【问题讨论】:

  • android:configChanges="orientation|screenSize" 在您的活动场所的 manifest.xml 中使用这一行。它解决了我的问题

标签: android imageview landscape


【解决方案1】:

当然可以,因为图像的尺寸可能适合纵向屏幕。如果您想查看全部,您需要:

  • 减小图像视图的大小
  • 使用视图中组件的“权重”属性
  • 如果要使用滚动,请添加滚动视图

【讨论】:

  • 权重属性仅用于线性布局。我试过了,但是如果我使用固定的布局权重,它可能会在横向模式下工作,但在纵向模式下却不能正常工作。当方向改变时,也许我应该通过代码改变它?最佳做法是什么?谢谢
【解决方案2】:

这是因为与横向模式下的屏幕高度相比,纵向模式下的屏幕高度更高。元素不会根据屏幕高度自动调整它们的位置。您应该处理所有方向/设备/平板电脑上的视图放置。

当设备处于横向模式时宽度更大。因此,请尝试在高度较小时更改视图的位置以利用宽度。

【讨论】:

    【解决方案3】:

    请试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:longClickable="false"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <Button
            android:id="@+id/btn_inbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Your Inbox" />
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/background" />
    
        <Button
            android:id="@+id/btn_qrscan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="Scan QR" />
    
    
    </LinearLayout>
    

    【讨论】:

    • 感谢您的回复。我试过了,但是当我从横向模式更改为纵向模式时,它仍然无法正确显示,差距太大。有没有一种方法可以让所有东西都适合屏幕,同时保持正确的比例?
    • 我已经更新了新的 xml 。你能用 imageview weight =1 试试这个吗
    • 创建一个同名的布局文件,放到layout-land文件夹中,与横向设计的组件相同
    【解决方案4】:

    我在 AndroidManifest.xml 中使用这些行解决了这个问题。

     android:configChanges="orientation|screenSize"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2020-12-02
      • 1970-01-01
      • 2019-08-13
      相关资源
      最近更新 更多