因此,当我动态地将文本视图添加到滚动视图时,我的按钮和编辑文本小部件会不断向上推。因此,scrollview的高度也在提升。我如何确保滚动视图是固定的,我已经查看了类似的问题,并试图实现它但不工作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:windowSoftInputMode="stateVisible|adjustPan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:hint="Enter name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"/>
<EditText
android:id="@+id/phone_num"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:hint="Enter Phone Number"/>
<Button
android:id="@+id/submit"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="@string/submit_contact"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:isScrollContainer="false"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
尝试将所有高度设置为0dp并将wheightSum提供给父视图。使用LinearLayout将按钮固定在一个固定高度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:windowSoftInputMode="stateVisible|adjustPan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2.75"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:hint="Enter name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"/>
<EditText
android:id="@+id/phone_num"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:hint="Enter Phone Number"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="vertical">
<Button
android:id="@+id/submit"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="test"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:isScrollContainer="false"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
尝试一下,如果它不起作用你可以发布你已经尝试过的东西和代码如何动态添加文本视图。祝好运