我有2个布局(main.xml和footer_layout.xml),我试图将footer_layout附加到main中的listview。我可以让它显示但是当我使用tvFooter.setText()时它不会设置文本;
main.xml中:
<?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" >
<ListView
android:id="@+id/listIssues"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
footer_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center" >
<TextView
android:id="@+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
MainActivity代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.footer_layout);
//Context context = getBaseContext();
tvFooter = (TextView)findViewById(R.id.footerText);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
mListViewIssues.addFooterView(footerView);
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
**请注意,我删除了“创建者”的代码,因为没有必要。
这个应用程序运行并显示数据,但它不会像我想要的那样在页脚中显示“DBVersion = 3”。
有什么想法吗?
答案 0 :(得分:2)
您不应将footer_layout设置为活动布局。正确的代码如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
View footerView =((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
tvFooter = (TextView)footerView.findViewById(R.id.footerText);
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
mListViewIssues.addFooterView(footerView);
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
答案 1 :(得分:2)
这一行:
tvFooter = (TextView)findViewById(R.id.footerText);
要求您找到的视图已在您的应用程序父布局“内部”。
尝试将findViewById()和setText()移到此行之后:
mListViewIssues.addFooterView(footerView);
或者更好(这可能实际上是必要的,我不确定它会以另一种方式工作)更改它,以便在tvFooter和父视图之间的一个视图上调用findViewById,即ListView,或者膨胀后的普通视图。像这样:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.footer_layout);
//Context context = getBaseContext();
tvFooter = (TextView)findViewById(R.id.footerText);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
mListViewIssues.addFooterView(footerView);
tvFooter = (TextView)footerView.findViewById(R.id.footerText);
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
请注意footerView.findViewById()的使用,如果您在footerView对象上显式调用它而不是仅仅从您的活动中调用它,我认为您可能会更好运,这与在您的活动父布局上调用它基本相同。