Android布局和名称空间
我对进入布局目录的所有“种类(活动、片段、视图)”的文件感到有点困惑。下面的代码可以使可扩展列表膨胀 活动:Android布局和名称空间,android,android-layout,Android,Android Layout,我对进入布局目录的所有“种类(活动、片段、视图)”的文件感到有点困惑。下面的代码可以使可扩展列表膨胀 活动: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); } 活动测试.xml <?xml version="1.0" encoding="utf-8"?&
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
活动测试.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<fragment
android:id="@+id/my_fragment"
android:name="com.xx.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
<?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">
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_exp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>
fragment_test.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<fragment
android:id="@+id/my_fragment"
android:name="com.xx.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
<?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">
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_exp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>
我发现,如果我不使用封闭的线性布局,它就找不到视图
ExpandableListView eview=(ExpandableListView)getView().findViewById(R.id.my\u exp\u list)
eview是空的,但当我将它括在LinearLayout中时,一切都正常 它应该适用于任何视图,如果没有比“它不起作用”更好的错误,就很难知道什么是错误的。一种可能是未设置
LayoutParams
,因为您应该使用此行:
return inflater.inflate(R.layout.fragment_test, container, false);
也可能它正在工作,但您没有在列表视图中放置任何内容
其他说明:
xmlns:
match\u parent
而不是fill\u content
。它们都是一样的,但谷歌为了更清晰而更改了名称activity\u foo.xml
,fragment\u foo.xml
,view\u bar.xml
等等。更糟糕的问题是,所有视图ID都位于同一名称空间中!如果能够执行R.id.activity\u foo.ok\u按钮
,那就太好了,因为当您键入R.id.
并且自动完成显示每个版面中的每个id时,它会让人感到困惑
除了给你的id命名之外,没有其他方法可以解决这个问题,比如
id=“@+id/activity\u foo\u ok\u button”
,但我认为这太繁琐和冗长了。谢谢你的回答。有关详细信息,请查找编辑。您可能应该将的代码粘贴到ActivityCreated()
上,因为这就是错误所在。