【问题标题】:Scaling an ImageView when the soft keyboard opens软键盘打开时缩放 ImageView
【发布时间】:2020-11-04 21:36:02
【问题描述】:

我正在创建一个选项卡式活动。只要选择了第一个选项卡,软键盘就应该可见。我通过将此行添加到活动标记中的清单文件中来实现:

android:windowSoftInputMode="stateVisible|adjustResize"

随着键盘的打开,布局空间会缩小。 ImageView 占用的空间最多。我希望它随着布局大小而缩小,以允许其他 2 个视图(应保持相同大小)适合屏幕。但是,虽然软输入模式设置为adjustResize,但ImageView 在键盘打开后仍保持其大小。这是当前布局和我想要实现的布局的比较(ImageView 是红色矩形): comparison

我的片段的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".DataInputFragment"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:src="@drawable/image"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/input_label_text"/>

        <namespace.InputEditText
            android:id="@+id/input_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            <requestFocus/>
        </namespace.InputEditText>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/submit"
    </LinearLayout>
</RelativeLayout>

我的活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TabLayout">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_1" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_2" />

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

如何强制 ImageView 根据屏幕大小调整大小以使所有视图都适合屏幕?

[编辑]:我尝试创建一个 ConstraintLayout,但没有解决问题(ImageView 仍保持其原始大小):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
    android:orientation="vertical"
    tools:context=".DataInputFragment">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitStart"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/input_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_label_text"
        app:layout_constraintTop_toBottomOf="@id/img"/>

    <namespace.InputEditText
        android:id="@+id/input_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/input_label">
        <requestFocus />
    </namespace.InputEditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/submit"
        app:layout_constraintTop_toBottomOf="@id/input_edit_text"/>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    我使用“Tabbed Activity”模板和uploaded it to GitHub,在 Android Studio 4.0 上从头开始创建了一个 Android 项目。

    调整活动大小时发生的调整大小没有问题。

    我改变了什么?

    1. manifest.xml 我添加了android:windowSoftInputMode="adjustResize"
    2. 我修改了默认的layout for the fragment in the tab,使其包含ImageView(称为imageToShrink)和EditText(称为editLayout)。
    3. 我保持现有的 TextView 不变,除了因为它在顶部,我将它设置为垂直链的 HEAD,并给它一个 0.0 BIAS 以使其在顶部而不是中心对齐。

    当你点击底部的 cyan EditText 时,键盘会弹出(我只设置了数字,所以它更高了),你可以看到图像在新尺寸后是如何重新绘制的重新计算。

    这是它打开时的样子:(漂亮的调色板!)

    这就是当我点击“编辑字段”弹出键盘时的样子:

    【讨论】:

      【解决方案2】:

      你的ImageView 不应该有wrap_content 属性,因为它总是有恒定的大小并且不能自动调整大小。我建议使用ConstraintLayoutapp:layout_constraintDimensionRatio。其他选项是使用LinearLayoutweight(不推荐)。

      Ant 第三个选项是检测键盘何时可见,何时不可见 ViewTreeObserver.OnGlobalLayoutListener 并以编程方式执行操作。

      试试这个:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout 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"
          tools:context=".DataInputFragment">
      
          <ImageView
              android:id="@+id/img"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:adjustViewBounds="true"
              android:scaleType="fitStart"
              android:src="@drawable/image"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintDimensionRatio="3:1"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.0" />
      
          <TextView
              android:id="@+id/input_label"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/input_label_text"
              app:layout_constraintTop_toBottomOf="@id/img" />
      
          <EditText
              android:id="@+id/input_edit_text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@id/input_label">
      
              <requestFocus />
          </EditText>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/submit"
              app:layout_constraintTop_toBottomOf="@id/input_edit_text" />
      
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

      • 感谢您的回复。你能用 ConstraintLayout 的方式帮我做这件事吗?我根据您的建议对布局进行了更改,但结果仍然相同。我在问题中粘贴了我的新代码。
      • 我尝试了更新的答案,但图像仍将保持相同大小。我终于使用@Martin Marconcini 回答解决了这个问题。感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2021-10-16
      • 2017-11-23
      • 2016-12-21
      • 1970-01-01
      相关资源
      最近更新 更多