Android layout 在线性布局视图中折叠和滚动视图
我试图在Android布局中创建一个非常特定的行为,当软键盘打开时,包含两个片段,如下面从左到右所示 片段A是一个需要软键盘输入的表单,片段B是一个摄影幻灯片,只是作为视觉填充 蓝色轮廓表示根视图。碎片A是固定高度,碎片B填充剩余的垂直空间。当键盘打开时,片段B折叠直到其高度为0,此时片段A可滚动 我尝试了以下方法:Android layout 在线性布局视图中折叠和滚动视图,android-layout,android-layout-weight,Android Layout,Android Layout Weight,我试图在Android布局中创建一个非常特定的行为,当软键盘打开时,包含两个片段,如下面从左到右所示 片段A是一个需要软键盘输入的表单,片段B是一个摄影幻灯片,只是作为视觉填充 蓝色轮廓表示根视图。碎片A是固定高度,碎片B填充剩余的垂直空间。当键盘打开时,片段B折叠直到其高度为0,此时片段A可滚动 我尝试了以下方法: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:o
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/FragmentB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
在该视图中,当软键盘打开时,碎片B保持在相同的高度,而碎片A折叠
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/FragmentB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
我在这里找到了一个解决方案:
我的结果代码是:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"> <!-- FillViewport ensures the scrollview doesn't wrap the content -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"> <!-- Layout weight 1 means the LinearLayout will at least fill the scrollview, or wrap content if too big -->
<FrameLayout
android:id="@+id/FragmentB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>