gpt4 book ai didi

android - ListView 将其他 View 推离屏幕

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:21 24 4
gpt4 key购买 nike

我正在努力使布局看起来正确,并且我已尝试生成我的问题的最短、最小的示例。

我的目标是在屏幕的顶部和底部有一个页眉和页脚 View ,在两者之间有一个 ListView,还有另一个 View (我们称它为 label ,它是屏幕截图中的灰色框)就在 ListView 下方。此标签和页脚应始终在 ListView 需要滚动时显示。

视觉效果

ListView 不需要滚动时(这是正确的):

when the ListView doesn't need to scroll

ListView 需要滚动时,页脚和灰色框被推离屏幕(错误):

when the ListView needs to scroll

布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"/>
<!-- Used to push the footer to the bottom -->
<View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"/>
</LinearLayout>

测试 Activity

public class TestActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
items.add("four");
items.add("five");
items.add("six");
items.add("seven");
items.add("eight");
items.add("nine");
items.add("ten");
setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, items));
setContentView(com.myproject.android.R.layout.test);
}
}

我尝试了几种不同的方法,比如给 ListView layout_weight="1" 并删除空白的 View用于将页脚推到底部。这几乎是我想要的,它在 ListView 滚动时保持页脚和标签可见,但是当它只有 1 或 2 个项目时,我需要 ListView 。我也曾尝试使用 RelativeLayout,但没有成功。我想我完全误解了事情。

编辑

这是我对 RelativeLayout 的尝试,但仍然不正确。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"
android:id="@+id/header"
/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_below="@id/header"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"
android:layout_below="@android:id/list"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"
android:layout_alignParentBottom="true"
android:id="@+id/footer"/>
</RelativeLayout>

相对布局(仍然错误):

relative layout attempt

最佳答案

android:layout_weight="1" 添加到 ListView 。这将使它成为 LinearLayout 中最大的元素,而不会将其他元素推离屏幕。

关于android - ListView 将其他 View 推离屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com