有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Android中使用多个带有include标记的布局

我正在尝试创建一个标题栏,该标题栏在整个应用程序中都是相同的,并且为我的每个活动创建了如下布局:

梅因。xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
 安卓:orientation="vertical"
 安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent">
 <include layout="@layout/title_bar_layout" 安卓:id="@+id/title_bar_layout" />
 <include layout="@layout/main_body" 安卓:id="@+id/main_body" />
</LinearLayout>

主体。xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
 安卓:id="@+id/main_table" 安卓:layout_width="fill_parent"
 安卓:layout_height="fill_parent" 安卓:stretchColumns="1"
 安卓:orientation="vertical">
</TableLayout>

我的onCreate包含:

setContentView(R.layout.main);
TableLayout t = (TableLayout) findViewById(R.id.main_table);

但t总是以空结尾


共 (1) 个答案

  1. # 1 楼答案

    尝试一下:

    TableLayout t = (TableLayout) findViewById(R.id.main_body);
    

    从这个page

    you can use android:id to specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined