gpt4 book ai didi

Android ScrollView 高度不变

转载 作者:行者123 更新时间:2023-11-30 01:39:19 25 4
gpt4 key购买 nike

我有一个包含以下 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>

顶部 View 是 RelativeLayout,主要内容有一个 ScrollView(其中包含一个 EditText)和自定义键盘 View 。当用户点击 EditText 时自定义键盘弹出。

它在大多数情况下都能完美运行,但如果在 EditText 中输入的文本很大,它会被自定义键盘 View 覆盖。所以那个时候,我需要调整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 值。

我错过了什么?

最佳答案

放置属性

android:layout_above="@+id/yourKeyboardLayout" 

在你的 ScrollView 中。

关于Android ScrollView 高度不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34708668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com