如何防止软键盘调整背景图片大小

26

我有一个典型的带有 edittext 和按钮的 listview,位于活动底部。

当我点击 edittext 时,软键盘将出现,我可以滚动 listview 中的项目,但它会重新调整我的背景图像大小。

我尝试使用 android:windowSoftInputMode="adjustResize",但没有任何区别。

我尝试使用 android:windowSoftInputMode="adjustPan"。图像不会被压缩,整个布局会向上移动并且我会失去标题栏。只有当列表项超过布局大小时,我才能滚动列表。

基本上,我想保留标题栏,保持背景图片不被调整大小,并允许滚动列表项。是否有人成功做到这一点?谢谢!

12个回答

46

对于列表视图,你需要使用:

android:isScrollContainer="false"

并将这段代码添加到你的manifest.xml文件中对应的活动中

android:windowSoftInputMode="adjustPan"

1
尝试了许多解决方案,最终为SCROLL-VIEW找到了可行的方法。非常感谢。 - Chintan Raghwani
谢谢!只需在我的布局文件中添加android:isScrollContainer="false"到弹出的布局中,问题就解决了 :) - JazzyJ

26

我尝试了很多方法来解决这个问题。android:windowSoftInputMode="adjustPan"会使整个屏幕随着键盘移动。通常我们在顶部有一个屏幕标题。使用此标志,它也将超出可见区域,从而导致糟糕的用户体验。我使用 android:windowSoftInputMode="adjustResize" 这将调整整个屏幕大小,但它会导致与 @Maurice 在问题中所述相同的问题。

所以这是我的最终解决方案:

在清单文件中

android:windowSoftInputMode="adjustResize|stateAlwaysHidden"

在XML中

不要在此处设置任何背景,并将您的视图放在ScrollView下面。

在Java中

您需要将背景设置为窗口:

getWindow().setBackgroundDrawableResource(R.drawable.bg_wood) ;

3
如果您希望元素可以自适应大小,同时背景保持不变,那么这就是正确的答案。此回答值得更多关注。+1 - Levite
1
太棒了!回答得真好! - gonzalomelov
1
天啊,我已经到处寻找这个答案了。这是有史以来最好的解决方案。使用Java而不是XML。 - Prasanth Louis
1
谢谢,伙计,它在活动中运行 :) ,我只想问如何为片段设置背景?当我在FragmentActivity中使用getWindow().setBackgroundDrawableResource时,整个页面都会改变。 - Iam ByeBlogs
运行得非常好!!只需在活动中设置背景图像。 - Tara

9

请前往Androidmanifest.xml文件并:

activity name="activityname" android:windowSoftInputMode="stateVisible|adjustPan"

1
但是那会调整背景图片的大小。 - Ajith M A

5

无法防止软键盘调整背景图像的大小。

使用scaleType(matrix)并准备适合的图像。

<?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="fill_parent"
  android:id="@+id/RelativeLayoutchat"
  >

  <LinearLayout
    android:orientation="horizontal"
    android:gravity ="clip_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true">

   <ImageView
        android:gravity ="clip_horizontal"
        android:id="@+id/chat_bak_img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix">
   </ImageView>

   </LinearLayout>

  <LinearLayout  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

  <ListView
        android:id="@+id/chat"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:transcriptMode="alwaysScroll"
        android:divider="#000000"
        android:clickable="false"
        android:layout_weight="9"
        android:cacheColorHint="#00000000"/>



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        style="@android:style/ButtonBar"
        android:gravity="center"
        android:paddingLeft="3dip"
        android:paddingTop="3dip"
        android:paddingRight="3dip">
        <EditText
            android:id="@+id/chatMsg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="5"
            android:enabled="true"
            android:textSize="17sp"
            android:maxLength="150"/>
        <Button
            android:id="@+id/sendMsg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/send_withSpace"
            android:layout_weight="4"/>
    </LinearLayout>

  </LinearLayout>

</RelativeLayout>

嗯,我认为adjustPan通常会防止背景调整大小。 - Ted

4
使用android:windowSoftInputMode="adjustResize",并在一个ScrollView中包裹你的背景ImageView即可。
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:overScrollMode="never">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/> 
</ScrollView>

1
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" android:overScrollMode="never"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" app:srcCompat="@drawable/myimage" /> </ScrollView> - Softlion

2

我遇到了与ImageView调整大小的问题,当键盘弹起时,我通过在Android清单文件中添加android:windowSoftInputMode="adjustPan"来解决该问题。以下是我的布局示例,例如frg_write_social:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frg_writeyourpost_ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:isScrollContainer="false"
android:scrollbars="none" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/frg_writeyourpost_rl_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/frg_writeyourpost_ll_bottum"
        android:layout_alignParentTop="true"
        android:minHeight="400dip" >

        <ImageView
            android:id="@+id/frg_writyourpost_iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:scaleType="fitXY" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/frg_writeyourpost_ll_bottum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/white"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="10dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:text="Write your post"
                android:textColor="#727F8D"
                android:textSize="10sp" />

            <com.example.views.CustomEditText
                android:id="@+id/frg_writeyourpost_et_message"
                android:layout_width="match_parent"
                android:layout_height="@dimen/slide_image"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@null"
                android:inputType="textMultiLine"
                android:maxHeight="@dimen/slide_image"
                android:maxLines="3"
                android:textSize="11sp" >
            </com.example.views.CustomEditText>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:orientation="horizontal" >

                <TextView
                        android:id="@+id/frg_writeyourpost_tv_totalcharacter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="0"
                    android:textColor="#727F8D"
                    android:textSize="8sp" />

                <TextView
                 android:id="@+id/frg_writeyourpost_tv_characterLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:gravity="center"
                    android:text="character"
                    android:textColor="#727F8D"
                    android:textSize="8sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <RadioGroup
                android:id="@+id/frg_writepost_fbtw_rggroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/frg_writepost_rb_facebook"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/fb_check_selector"
                    android:button="@android:color/transparent"
                    android:gravity="center"
                    android:padding="10dip"
                    android:text="@string/post_for_facebook"/>

                <View
                    android:layout_width="5dip"
                    android:layout_height="match_parent"
                    android:background="@android:color/transparent"/>

                <RadioButton
                    android:id="@+id/frg_writepost_rb_twitter"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/tw_check_selector"
                    android:button="@android:color/transparent"
                    android:gravity="center"
                    android:padding="10dip"
                    android:text="@string/post_for_twitter"/>
            </RadioGroup>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

如果有可能动态加载图片,您可以在ImageView中加载图片时使用Picaaso调整图片大小。

1

在RelativeLayout中使用fill_parent和centerCrop属性的ImageView。像这样;

<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"
    >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/yazisma_arka_plan"
        android:scaleType="centerCrop"
        />
    <ListView
        android:id="@+id/messages_view"
        android:layout_width="fill_parent"...

1

我在StackOverflow上阅读了很多答案,并从中吸取了一些来使我的应用程序正常工作:

  1. 在我的activity_main.xml中完全没有ScrollView

  2. AndroidManifest.xml中为

    <activity android:name=".MainActivity" ...>

添加了这个属性:

android:windowSoftInputMode="adjustResize"
  1. MainActivity.javaonCreate()中,我添加了getWindow().setBackgroundDrawableResource(R.drawable.app_bg);

对于我来说,这个方法是其他尝试都没用的。如果你也尝试过其他方法但没有成功,希望这个方法能对你有用。


0

嗯,让我看看...先看图片!

触摸前查看 触摸后查看 布局文件为act_login.xml。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bkgd"
    android:orientation="vertical"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#0fff"
        android:scrollbars="none"
        android:listSelector="#0fff"
        android:divider="#0fff"
        android:dividerHeight="0dp"
        android:isScrollContainer="true"
        />
    <!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
    <!--android:isScrollContainer="true"-->
    <!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->

    <LinearLayout
        android:id="@+id/loginItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            >

            <ImageView
                android:id="@+id/loginLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="35dp"
                android:layout_marginTop="35dp"
                android:scaleType="fitCenter"
                android:src="@drawable/logo"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingBottom="35dp"
                >

                <View
                    android:layout_width="0dp"
                    android:layout_height="1px"
                    android:layout_weight="1"
                    android:background="#Ffff"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="6dp"
                    android:text="WELCOME"
                    android:textColor="#Ffff"
                    android:textSize="14sp"
                    />

                <View
                    android:layout_width="0dp"
                    android:layout_height="1px"
                    android:layout_weight="1"
                    android:background="#Ffff"
                    />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/login_et_long"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                >

                <TextView
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:text="账号:"
                    android:textColor="#Ffff"
                    android:textSize="16sp"
                    />

                <EditText
                    android:id="@+id/loginUsername"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null"
                    android:hint="请输入手机号"
                    android:imeOptions="actionNext"
                    android:inputType="text"
                    android:singleLine="true"
                    android:textColor="#F000"
                    android:textSize="16sp"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="45dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/login_et_long"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                >

                <TextView
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:text="密码:"
                    android:textColor="#Ffff"
                    android:textSize="16sp"
                    />

                <EditText
                    android:id="@+id/loginPassword"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null"
                    android:hint="请输入密码"
                    android:imeOptions="actionGo"
                    android:inputType="textPassword"
                    android:singleLine="true"
                    android:textColor="#F000"
                    android:textSize="16sp"
                    />
            </LinearLayout>

            <TextView
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/login_button"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/str_login"
                android:textColor="#Ffff"
                android:textSize="19sp"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

相关的Java代码。

EditText etUsername, etPassword;
EditText etFocus;
ViewGroup base;
int mSelection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    base=(ViewGroup)getLayoutInflater().inflate(R.layout.act_login, null);
    setContentView(base);
    getWindow().setBackgroundDrawable(base.getBackground());
    base.setBackground(null);
    super.onCreate(savedInstanceState);
}

@Override
protected void initViews() {
    etUsername=(EditText)findViewById(R.id.loginUsername);
    etPassword=(EditText)findViewById(R.id.loginPassword);

    final ViewGroup item=(ViewGroup)findViewById(R.id.loginItem);
    base.removeView(item);
    final View[] items=new View[item.getChildCount()];
    for(int i=0; i<item.getChildCount(); i++) items[i]=item.getChildAt(i);
    item.removeAllViews();

    ListView lv=(ListView)base.getChildAt(0);
    lv.setAdapter(new BaseAdapter() {
    public View getView(int i, View v, ViewGroup vg) {
        if(etFocus!=null && getCurrentFocus()!=etFocus){
            etFocus.requestFocus();
            etFocus.setSelection(mSelection);
            //etFocus=null;
        }
        return items[i];
    }
    public int getCount() {return items.length;}
    public Object getItem(int position) {return null;}
    public long getItemId(int position) {return 0;}
    });

    /*View.OnTouchListener listener=new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            if(e.getAction()!=MotionEvent.ACTION_UP) return false;
            etFocus=(EditText)v;
            mSelection=etFocus.getSelectionEnd();
            ZZ.z("_________________ mSelection="+mSelection);
            return false;
        }
    };
    etUsername.setOnTouchListener(listener);
    etPassword.setOnTouchListener(listener);*/

    View.OnFocusChangeListener listener1=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                etFocus=(EditText)v;
                mSelection=etFocus.getSelectionStart();
                ZZ.z("_________________ mSelection="+mSelection);
            }
        }
    };
    etUsername.setOnFocusChangeListener(listener1);
    etPassword.setOnFocusChangeListener(listener1);
}

AndroidManifest 部分。

    <activity
        android:name=".ui.activity.ActLogin"
        android:label="@string/str_login"
        android:theme="@style/myTheme"
        />
    <!--android:windowSoftInputMode="adjustResize"-->

样式部分。

<style name="myTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

0
对于那些不理解的人:
双重检查你的xml是否已经设置了@drawable(这是我的错误,我通过android manifest进行设置,但它已经在xml的根目录中设置了)。
之后创建一个带有背景的主题样式。
<style name="AppThemeWBackgroundG" parent="AppTheme">
        <item name="android:windowBackground">@drawable/background_com_gradiente</item>
</style>

然后在你的 AndroidManifest.xml 的活动中:

<activity
   android:name=".TelaLoginFuncionario"
   android:windowSoftInputMode="adjustResize|stateAlwaysVisible"
   android:theme="@style/AppThemeWBackgroundG"
    android:fitsSystemWindows="true"
/>

就是这样:活动进入一个屏幕,键盘弹出,调整项目的大小,而背景保持正常大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接