Как запретить Soft Keyboard изменять размер фонового изображения - PullRequest
19 голосов
/ 15 марта 2011

У меня есть типичный listview с edittext и кнопкой в ​​нижней части упражнения.

Когда я нажимаю на edittext, появляется программная клавиатура, я могу прокручиватьэлементы в listview, но он изменяет размеры моего фонового изображения.

Я пробовал android:windowSoftInputMode="adjustResize", но без разницы.

Я пробовал android:windowSoftInputMode="adjustPan".Изображение не сдавливается, весь макет смещается вверх, и я теряю строку заголовка.Я могу прокручивать список только в том случае, если элементы списка превышают размер макета.

В основном я хочу сохранить строку заголовка, сохранить фоновое изображение без изменения размера и разрешить прокрутку элементов списка.Кому-нибудь удалось это сделать?Спасибо!

Ответы [ 12 ]

0 голосов
/ 27 июня 2017

Я прочитал много ответов по SO и взял несколько из многих, чтобы заставить мое приложение работать:

  1. Нет ScrollView вообще в моих activity_main.xml
  2. В AndroidManifest.xml добавлен этот атрибут к тегу

    <activity android:name=".MainActivity"

:

`android:windowSoftInputMode="adjustResize"`
В MainActivity.java внутри onCreate() Я добавил getWindow().setBackgroundDrawableResource(R.drawable.app_bg);

Это работает для меня, когда все остальные полные ответы, которые я попробовал, не сработали.Если вы добрались до этого места, я надеюсь, что это сработает и для вас.

0 голосов
/ 23 июня 2016

Э-э, дай мне посмотреть ... сначала картинки!

View before touched View after touched Файл макета 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>
...