键盘打开时滚动活动页面

时间:2016-03-31 08:51:21

标签: java android layout scroll

我希望当软键盘打开时,活动页面会滚动。

为此,我在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"

    tools:context="com.example.trying.MainActivity" 
    >


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >

        <RelativeLayout
            android:id="@+id/LayoutBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
             android:background="@drawable/background" 
           >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_world" >

              <requestFocus />
           </EditText>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

并在代码中更改了realativeLayout高度:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Display display = getWindowManager().getDefaultDisplay();
         Point size = new Point();
         display.getSize(size);
         int height = size.y;

         int x=getActionBarHeight();//in this line I get the actionBar height programicaly the function is in the end of page


         RelativeLayout l= (RelativeLayout)findViewById(R.id.LayoutBackground);
         l.getLayoutParams().height = (height-x);//the layout size = size of full screen - size of actionBar




    }

    //I saw this function in stackOverflow site. function to get the actionBar height
    public int getActionBarHeight() {

            TypedValue tv = new TypedValue();
            getApplicationContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
            int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
            return actionBarHeight;
        }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trying"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
             android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

idia是布局高度取决于屏幕尺寸(所有设备),动作栏也是如此。

我的问题是,当我运行应用程序时,背景很小。 它似乎动作栏的大小下降了两次,

2 个答案:

答案 0 :(得分:0)

将以下代码行添加到特定活动的清单

android:windowSoftInputMode="adjustResize"

答案 1 :(得分:0)

您不需要在布局中添加ScrollView。从xml中删除它。然后在AndroidManifest.xml中,在您的活动中添加以下行:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
          android:configChanges="keyboardHidden"
        android:windowSoftInputMode="adjustPan|stateAlwaysHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>