我对ListView
的问题是只有ListView可滚动而不是整个屏幕。
例如,如果我有一个类似下面的布局,只有屏幕的ListView部分是可滚动的,而我希望整个屏幕滚动:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="24dp"
android:paddingBottom="24dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum."
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
是否可以填充LinearLayout
,如上所示,以使整个屏幕可滚动?
答案 0 :(得分:1)
有一种更简单的方法来处理这个问题,ListView提供了addHeaderView(View)
方法,因此你可以这样做:
创建header_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum."/>
</LinearLayout>
然后有你的main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="24dp"
android:paddingBottom="24dp">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
然后在你的MainActivity.java
中 public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// reference your list
ListView listView = (ListView) findViewById(R.id.listview);
// Inflate the header and add the view
View headerView = LayoutInflater.from(MainActivity.this).inflate(R.layout.header_view, null);
listView.addHeaderView(headerView);
}
}
请参阅http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
答案 1 :(得分:1)
是的,这是可能的。
简单的方法 - 将ScrollView
中的所有内容包裹起来,这是丑陋的,不推荐但可行。
更好的方式 - ListView
有addHeaderView(View v)
和addFooterView(View v)
如果你想拥有以上内容,请使用标题,如果你想要以下内容,只需使用页脚。在您的情况下,将这两个TextViews
带到新的布局文件中。像这里充气吧
LayoutInflater.from(context).inflate(R.layout.new_created_layout_with_these_two_text_views,null)
并将创建的视图添加为标题。
答案 2 :(得分:0)
您要做的事情有两个问题
1)您无法滚动LinearLayout
。要滚动LinearLayout
,您必须插入ScrollView
作为其第一个且唯一的孩子,然后将所有子视图放在ScrollView
2)你想要做的事情是行不通的。 ScrollView
和ListView
只是内容的窗口。实际的ListView
本身只与它在屏幕上占用的空间一样大,而不是其中包含的内容的大小。如果您希望ListView
和TextView
的内容一起滚动,则应将TextView
添加到ListView
中,或者添加换行视图作为ListView
的标题视图。您的另一个选择是根本不使用ListView
,只是以编程方式将所有子项附加到ScrollView
。
注意:您不应使用ScrollView
和ListView
。选择一种方法并坚持下去。