Android ScrollView高度不变

时间:2016-01-10 17:40:20

标签: android android-layout height scrollview

我有一个包含以下XML的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/myScrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/myEditLinearLayout"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/myEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="8dip"
                android:paddingRight="8dip"
                android:maxLines="300"
                android:scrollHorizontally="false"
                android:hint="Type notes here..."
                android:inputType="textNoSuggestions|textMultiLine"
                android:layout_gravity="center_horizontal|center_vertical"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    <RelativeLayout
        android:id="@+id/layoutKbd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <android.inputmethodservice.KeyboardView
               android:id="@+id/myKeyboardView"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_centerHorizontal="true"
               android:focusable="true"
               android:focusableInTouchMode="true"
               android:keyPreviewLayout ="@layout/kbdpreview"
               android:visibility="gone" />
    </RelativeLayout>
</RelativeLayout>

顶视图是RelativeLayout,主要内容(包含EditText)和自定义键盘视图的ScrollView。当用户单击EditText时,会弹出自定义键盘。

它在大多数情况下都能很好地工作,但是如果在EditText中输入的文本很大,它将被自定义键盘视图覆盖。那时候,我需要调整ScrollView的高度。我是按照以下方式做到的:

// Activity's onCreate implementation
public void onCreate(Bundle savedInstanceState)
{
    ...

    EditText editText = (EditText) findViewById(R.id.myEditLinearLayout);
    editText.addTextChangedListener(m_tw);

    ...
}

private TextWatcher m_tw = new TextWatcher()
{
    @Override
    public void afterTextChanged(Editable s)
    {
    // This function returns if the custom keyboard is visible
    if (m_customKeyboard.isCustomKeyboardVisible())
    {
        int kbdHeight = m_customKeyboard.mKeyboardView.getHeight();
        int editTextBottom = m_editText.getBottom();
        ScrollView scrollView = (ScrollView) findViewById(R.id.myScrollview);

        Rect scrollBounds = new Rect();
        Log.d(TAG, "Setting scrollview height = " + (editTextBottom + kbdHeight + 20));
        scrollView.getHitRect(scrollBounds);
        Log.d(TAG, "Old scroll bounds: left = " + scrollBounds.left + " right = " + scrollBounds.right + " top = " + scrollBounds.top + " bottom = " + scrollBounds.bottom);

        scrollView.getLayoutParams().height = editTextBottom + kbdHeight + 20;

        Log.d(TAG, "Setting scrollview height = " + (editTextBottom + kbdHeight + 20));
        scrollView.getHitRect(scrollBounds);
        Log.d(TAG, "New scroll bounds: left = " + scrollBounds.left + " right = " + scrollBounds.right + " top = " + scrollBounds.top + " bottom = " + scrollBounds.bottom);
    }
    }
};

以上不起作用。在设置scrollView高度之前和之后,它显示了scrollBounds.bottom的相同值。

我错过了什么?

4 个答案:

答案 0 :(得分:1)

放置属性

android:layout_above="@+id/yourKeyboardLayout" 
滚动视图中的

答案 1 :(得分:0)

您可以尝试将android:windowSoftInputMode="adjustPan"放入清单中。如果你不喜欢这个结果,也许你可以使用其他一些平底锅。

答案 2 :(得分:0)

  

添加此内容   机器人:windowSoftInputMode = “adjustPan”    在您的清单中声明活动,您还可以添加滚动视图

答案 3 :(得分:0)

如果您想更改scrollview的LayoutParams,您应该在示例中使用以下代码:

ViewGroup.LayoutParams lp = scrollView.getLayoutParams();
lp.height = editTextBottom + kbdHeight + 20;
scrollview.setLayoutParams(lp);