Java 如何将ListView放置在页面内部?
我目前有一些刷卡页面,我希望在所有页面中都有包含不同数据的列表。Java 如何将ListView放置在页面内部?,java,android,xml,android-layout,listview,Java,Android,Xml,Android Layout,Listview,我目前有一些刷卡页面,我希望在所有页面中都有包含不同数据的列表。 我对android还是相当陌生,所以我尝试了所有可能的选择: 首先,我尝试在页面内部添加一些xml布局 第二,我尝试实用地创建和填充列表 以上这些都不适合我 首先,我想检查一下我是否理解这个概念: 滑动布局包含页面,数据可以通过getItem()Fragment类型内容传递给页面 现在如何实现我想要的: 我必须在我的layout/main.xml主页模板中添加LinearLayout子ListView,最后为TextView元素添
我对android还是相当陌生,所以我尝试了所有可能的选择:
滑动布局包含页面,数据可以通过
getItem()
Fragment
类型内容传递给页面
现在如何实现我想要的:我必须在我的
layout/main.xml
主页模板中添加LinearLayout
子ListView
,最后为TextView
元素添加一个子元素,并创建一个数据填充方法
我的推理是否有问题,这就是为什么我找不到答案,或者我只是没有找到正确的来源?
代码示例:main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MainActivity.java
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
...
您可以通过声明一个xml来实现这一点,比如说
listview.xml
,在其中定义您的listview
。
您可以按语法创建一个新的TextView
或创建一个新的布局,比如titleview.xml
,在其中使用TextView
编写标题视图
从语法上讲,您可以膨胀这两个视图,并将主布局父视图作为父视图传递给这些子视图
LayoutInflater inflater = getLayoutInflater();
ListView listview = inflater.inflate(R.layout.list_view, parent);
TextView textview = inflater.inflate(R.layout.textview, parent);
layout.addView(textview);
layout.addView(listview);
从哪里可以获得父变量和布局变量?我尝试了以下方法:RelativeLayout item=(RelativeLayout)findviewbyd(R.id.pager);视图列表=GetLayoutFlater()。充气(R.layout.list\u视图,空);item.addView(列表);不起作用。布局是您的主视图布局参考。就像你有一个布局,它显示屏幕上的标题和屏幕中间的ListVIEW。然后文本视图将成为您的第一个布局,然后声明另一个父布局可以是LinearLayout或RelativeLayout。在活动中获取此布局的引用。现在将膨胀视图添加到布局中。linearlayout.add(列表视图);如果您传递一个父视图,那么android将应用父-子视图的所有属性。否则,您可以传递null来代替parent。