Gra*_*yon 12 android android-layout android-linearlayout android-scrollview
我遇到了包含一个包含LinearLayout的ScrollView的Fragment问题.我正在尝试创建一种效果,其中LinearLayout具有白色背景,看起来像一张纸在彩色背景上滚动.我试图实现这一目标的方法是让ScrollView占据片段的整个空间,然后内部的LinearLayout必须android:layout_margin="16dp"创建"纸张"周围的空间.
这样,ScrollView的滚动条显示在彩色背景区域中,顶部的边距与内容一起滚动,底部的边距仅在到达末尾时滚动.
不幸的是,在这种配置中,ScrollView不会一直滚动到最后,实际上会截断底部的非常少量的文本.我怀疑ScrollView没有在其垂直滚动距离中考虑其孩子的边距.为了解决这个问题,我将LinearLayout包装在一个解决问题的FrameLayout中,但似乎是多余的.关于如何消除这个不需要的容器的任何指示将不胜感激.
注意:android:padding="16dp"在ScrollView上设置并删除边距不会产生所需的效果,因为无论滚动位置如何,填充都会连续显示在所有四个边上.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".ArticleFragment" >
<!-- This FrameLayout exists purely to force the outer ScrollView to respect
the margins of the LinearLayout -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_margin="16dp"
android:background="@color/page_background" >
<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="true" />
<TextView
android:id="@+id/article_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
</LinearLayout>
</FrameLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我记得ScrollView当内容布局有一个上边距时,不知何故没有把它放到它的内容的末尾.我用一个小黑客解决了这个问题,在结尾添加了一个空视图LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".ArticleFragment" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_margin="16dp" >
<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="true"
android:background="@color/page_background" />
<TextView
android:id="@+id/article_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:background="@color/page_background" />
<!-- Add a little space to the end -->
<View
android:layout_width="fill_parent"
android:layout_height="30dp" />
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我也在开头使用了类似的空视图,LinearLayout以完全避免上/下边距.
编辑:我刚刚意识到你还希望在到达视图结尾时显示"纸张"的结尾.在这种情况下,您可能希望将背景颜色设置为TextViews而不是布局本身.然后确保标题和文章之间没有边距(使用填充作为分隔).
编辑2:我应该学会检查问题的时间......好吧,也许这仍然有助于某人.:)
我会摆脱FrameLayout并将其LinearLayout子项ScrollView设置match_parent为布局高度。如果这仍然不起作用,请考虑将底部边距替换为View具有所需高度和背景的另一个任意值,作为LinearLayout.
| 归档时间: |
|
| 查看次数: |
6697 次 |
| 最近记录: |