【问题标题】:How to prevent other View being pushed out of the screen如何防止其他视图被推出屏幕
【发布时间】:2011-12-04 17:20:46
【问题描述】:

这是示例 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:layout_margin="5dip" android:background="#FF992222">
        <ScrollView android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <LinearLayout android:layout_height="wrap_content"
                android:orientation="vertical" android:layout_width="fill_parent">
                <TextView android:layout_width="wrap_content" android:text="TextView"
                    android:layout_height="wrap_content" android:textSize="24dip"></TextView>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:layout_margin="5dip" android:background="#FF555555">
        <TextView android:layout_width="wrap_content" android:text="TextView"
            android:layout_height="wrap_content" android:textSize="24dip"></TextView>
    </LinearLayout>
</LinearLayout>

结果是这样的:

如果你注意到的话,上面的LinerLayout 中的文本被LinearLayout 和ScrollView 包裹住了。

如果继续添加上面的内容,它会是这样的

底部线性布局将在 ScrollView 变为活动状态之前被推出屏幕并使第一个内容变为可滚动......我不希望发生这种情况......

在底部视图被推出屏幕之前,我应该怎么做才能使滚动视图像这样:

(这是经过编辑的图像,不是由 Android 布局编辑器创建的)

希望问题足够清楚.. 谢谢:)

【问题讨论】:

  • 为什么不把所有东西都放在一个滚动视图中。
  • 所以您根本不希望灰色文本视图移动而红色文本视图保持可滚动,对吗?

标签: android layout scrollview


【解决方案1】:

我花了很长时间才弄清楚这一点,网上没有任何信息,但我终于明白了。享受吧!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical"
 >

<LinearLayout
    android:id="@+id/LL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"                
    android:gravity="center_vertical"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:weightSum="1" >


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/TV1"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:fillViewport="true" >

        <TextView
            android:id="@+id/TV2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:layout_weight="0"
        android:orientation="horizontal" >

        <TextView android:layout_width="wrap_content" android:text="TextView"
        android:layout_height="wrap_content" android:textSize="24dip"></TextView>

    </LinearLayout>
</LinearLayout>

【讨论】:

  • 这应该是公认的答案,因为它有效。
【解决方案2】:

试试这个。关键是在线性布局中使用 layout_weight。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="fill_parent">
        ....
    </ScrollView>
    <TextView android:layout_width="fill_parent" android:text="TextView"
        android:layout_height="wrap_content" android:textSize="24dip"/>
</LinearLayout>

【讨论】:

  • 我知道我可以使用 layout_weight 实现最后的屏幕截图。但我想让第一个盒子变得灵活..如果内容只有 1 / 2 行,它会保持小,当内容太多时,它会滚动......
  • 如果在第一个 LinearLayout 中将 android:layout_height="fill_parent" 更改为 android:layout_height="wrap_content",第一个框将是灵活的。我认为。
  • 为我工作.. 谢谢
【解决方案3】:

好吧,如果我清楚地理解您的问题,您希望灰色的 TextView 保持静止,而红色的 textViews 可以滚动,如果我在这里错了,请纠正我。如果这就是你想要的,那么你可以使用 RelativeLayout 来实现。以下是产生类似输出的代码:
http://imageshack.us/photo/my-images/836/device20111011012652.png/
ListView 条目是可滚动的,而按钮(在您的情况下应该是 TextView)保持在原位。

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/newtask" android:layout_alignParentTop="true">
</ListView>

<Button android:layout_width="fill_parent" 
android:id="@+id/newtask" 
android:layout_height="wrap_content" 
android:text="Add New Task" android:layout_alignParentBottom="true">
</Button>
</RelativeLayout>  

只需将底部按钮更改为 TextView,它就会匹配您想要的。

【讨论】:

  • 哇太棒了....这不是我想要的答案...但它确实让我知道RelativeLayout可以做什么... :)在您的xml中,当android:layout_alignParentBottom="true"被调用时, relativeLayout 变成了 fill_parent,我不想这样做。相反,我摆脱了它并将android:layout_below="@+id/box1 添加到底部框以使其像我想要的那样...... :)
  • 哦不...它不能解决我的问题....这是我想要实现的...我想让第一个盒子变得灵活...所以当第一个盒子内容只有1 / 2行,它看起来像第一张照片。当内容太多时,它看起来像第三张图片......
  • 好吧,这可能有点实现,但如果你使用 ListViews(理想情况下你应该使用它),那么你可以覆盖 onScroll 监听器并检查列表中的最后一个条目可见与否。如果它是可见的,那么您必须以编程方式将灰色 TextView 的对齐方式设置为最后一项的底部,否则将对齐方式设置为屏幕底部(如答案所示)。查看listveiw最后一个条目的参考:stackoverflow.com/questions/5123675/…
  • 此答案中的视频链接已损坏。
  • @lubosz 您正在查看一个 6 岁的答案,显然不应再遵循该答案。过去,RelativeLayout 确实拥有此属性,但 6 年后情况发生了变化。与其在当时对一个完全合法的答案投反对票,不如考虑留下一个注释,说明这个答案可能不再有效,因为这对未来的访问者更有帮助。
猜你喜欢
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多