【问题标题】:ImageView resizes when keyboard openImageView 在键盘打开时调整大小
【发布时间】:2013-02-14 16:23:12
【问题描述】:

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/bgImage"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/todo"
    android:scaleType="fitXY"
    android:src="@drawable/default_wallpaper" />

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/title"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit_text_out"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:inputType="none" />

        <Button
            android:id="@+id/button_send"
            android:layout_width="70dp"
            android:layout_height="fill_parent"
            android:text="@string/send"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

当我点击EditText 软键盘打开时,我的ImageView 正在缩小。

我在清单文件中使用了android:windowSoftInputMode="adjustPan",但是通过使用它,它会将整个活动向上推,但我希望 ImageView 保持原样,并且当键盘打开时,ImageView 是从底部切开而不是像 WhatsApp 应用程序中那样从顶部切开。

任何帮助将不胜感激。谢谢你..

【问题讨论】:

    标签: android android-layout android-relativelayout android-softkeyboard


    【解决方案1】:

    删除父布局(RelativeLayout),使仅包含对话框的LinearLayout 成为父布局!另外,将android:windowSoftInputMode="stateVisible|adjustResize" 添加到AndroidManifest.xml

    【讨论】:

    • 好吧,如果我删除 RelativeLayout 那么如何将 IMageView 放在所有控件的背景中?
    【解决方案2】:

    我有一个类似的问题,其中一个自定义视图 - ParallaxBackground - 我已经适应了应该充当具有视差效果的背景图像,该视差效果由同一活动中的 ViewPager 的滚动驱动。

    换句话说,我有一个视图,它显示的图像宽度略大于屏幕的宽度,当用户沿ViewPager 滚动时,ParallaxBackground 中的图像也会滚动一点。

    现在,在我的情况下,我的 ViewPager 片段中有一些 EditText,当用户单击它们时,导致软键盘出现,ParallaxBackground(以及因此,背景图像)将重新调整大小并 挤压“违背我的意愿”。

    所以我为fixedWidthfixedHeight 添加了一个boolean isBackground,两个ints,并像这样覆盖了我的视图的onMeasure()

     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    
        if(isBackground) 
        // If this is being used as a background and is supposed to occupy the whole screen...
        {
                        // force the View to have the specified dimensions
            setMeasuredDimension(fixedWidth, fixedHeight);
        }
        // ...aply default measures...
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
     }
    

    我知道这不是一个“漂亮”的解决方案,但是,即使我的回答与您的问题不是 100% 相关,您可能想尝试扩展 ImageView 类并覆盖 onMeasure

    然后,在您的onCreate 方法中,您可以将isBackground 设置为true,并为fixedWidthfixedHeight 提供您测量的屏幕宽度和高度的值。

    【讨论】:

      【解决方案3】:

      从 Java 而非 XML 添加背景图片:

      getActivity().getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.splash_image));
      

      在清单中添加

      android:windowSoftInputMode="adjustResize|stateHidden"
      

      如果必须在打开屏幕时隐藏键盘,可以根据需要编写隐藏状态

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-17
        • 2019-11-17
        • 1970-01-01
        • 2016-10-11
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多