小编Gra*_*yon的帖子

ScrollView不会滚动到内部LinearLayout底部边距的末尾

我遇到了包含一个包含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)

android android-layout android-linearlayout android-scrollview

12
推荐指数
2
解决办法
6697
查看次数

以完全公平的方式从目录树中随机选择文件

我正在寻找一种从目录树中随机选择文件的方法,使得任何单个文件与所有其他文件具有完全相同的概率.例如,在以下文件树中,每个文件应有25%的可能性被选中:

  • /一些/父母/ DIR /
    • Foo.jpg
    • sub_dir /
      • Bar.jpg
      • Baz.jpg
      • another_sub /
        • qux.png

我在编写应用程序其余部分时使用的临时解决方案是具有如下函数:

def random_file(dir):
    file = os.path.join(dir, random.choice(os.listdir(dir)));
    if os.path.isdir(file):
        return random_file(file)
    else:
        return file
Run Code Online (Sandbox Code Playgroud)

然而,这显然会使结果产生偏差,这取决于它们在树中的位置以及目录中有多少兄弟姐妹,因此它们最终会被选中以下概率:

  • /一些/父母/ DIR /
    • Foo.jpg - 50%
    • sub_dir /(50%)
      • Bar.jpg - 16.6%
      • Baz.jpg - 16.6%
      • another_sub /(16.6%)
        • qux.png - 16.6%

该函数的上下文是在我正在编写的后台轮换应用程序中,因此从结果中过滤掉不需要的文件扩展名的能力将是一个额外的好处(尽管如果不是文件类型,我可以通过再次选择来强制执行此操作我想......如果存在大量"错误"类型的文件,那会变得混乱.

python

7
推荐指数
2
解决办法
3290
查看次数