我使用这段代码将我的布局划分为 2 个 ScrollView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:background="@drawable/ashokb"
android:layout_weight="4" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/flagc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="-50dp"
android:background="@drawable/ic_menu_share" />
</LinearLayout>
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#000000"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:background="@drawable/ashokb"
android:layout_weight="1" >
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Medium Text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
这在 GraphicalLayout 中显示如下
在设备和模拟器中, View 在对话框中如我所愿变得正确:
问题: 如果 textview 中的文本很小,那么第一个 scrollview 会像预期的那样表现正常,但是如果 texview 中的文本很大,那么即使完全滚动后,完整的文本也不可见,相反它在滚动末尾显示空白。如视频所示
在最后点击的项目中看到,第一个 ScrollView 没有完全显示其内容。
我希望你把这个问题弄清楚。
有什么解决办法吗?
最佳答案
基本上,您在布局文件中设置了许多不必要的属性。但是你的问题是由 android:layout_gravity
属性设置在你的顶部 ScrollView
中的第一个 LinearLayout
引起的:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
删除 android:layout_gravity="center"
将解决此 ScrollView
中内容被裁剪的问题。
顺便说一句:如果其中的内容小于 ScrollView
并且您想将其居中放置,您应该使用不同的方法。您不想让子项直接在 ScrollView
中居中,但您应该用它的唯一子项填充 ScrollView
并将内容居中在这个子项中。通常你会在这个 LinearLayout
和 android:gravity="center"
上设置 android:layout_height="match_parent"
。但在 ScrollView
中,您不能使用这种方法 - ScrollView
直接子项的高度应始终为 wrap_content
并将其设置为 match_parent
不会工作。然而在 ScrollView
中有一个属性允许这样的行为,它被称为 setFillViewport(boolean fillViewport)您应该将其设置为 true,如下所示:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
除了 ScrollView
中的以下几行之外,没有任何意义。它基本上将此 ScrollView
的引力设置为 center
,但由于它与其父级的大小相匹配 - 它不能位于中心。
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
顺便说一句。第二个 ScrollView
不应将 android:layout_height
设置为 wrap_content
。请将其更改为 match_parent
。
此外,您真的应该尝试简化布局和属性数量(尤其是所有“重力”:)
关于android - ScrollView 滚动,但不完全显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036546/