Android ScrollView 高度不变
Android ScrollView height not changing
我的布局如下 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);
}
}
};
以上方法无效。 scrollBounds.bottom 在设置 scrollView 高度之前和之后显示相同的值。
我错过了什么?
放 属性
android:layout_above="@+id/yourKeyboardLayout"
在你的滚动视图中。
您可以尝试将 android:windowSoftInputMode="adjustPan"
放入您的清单中。如果您不喜欢这个结果,也许您可以使用其他锅。
Add this
android:windowSoftInputMode="adjustPan"
in your manifest where activity is declare and you can also add in scroll view
如果你想改变scrollview的LayoutParams,你应该在你的例子中使用下面的代码:
ViewGroup.LayoutParams lp = scrollView.getLayoutParams();
lp.height = editTextBottom + kbdHeight + 20;
scrollview.setLayoutParams(lp);
我的布局如下 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);
}
}
};
以上方法无效。 scrollBounds.bottom 在设置 scrollView 高度之前和之后显示相同的值。
我错过了什么?
放 属性
android:layout_above="@+id/yourKeyboardLayout"
在你的滚动视图中。
您可以尝试将 android:windowSoftInputMode="adjustPan"
放入您的清单中。如果您不喜欢这个结果,也许您可以使用其他锅。
Add this android:windowSoftInputMode="adjustPan" in your manifest where activity is declare and you can also add in scroll view
如果你想改变scrollview的LayoutParams,你应该在你的例子中使用下面的代码:
ViewGroup.LayoutParams lp = scrollView.getLayoutParams();
lp.height = editTextBottom + kbdHeight + 20;
scrollview.setLayoutParams(lp);