我的原始布局 xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:background="#e4e8ed"
android:orientation="vertical"
android:padding="0px" >
<include
android:id="@+id/tabBar"
layout="@layout/tab" />
<Button
android:id="@+id/nist"
android:layout_width="301dp"
android:layout_height="63dp"
android:text="Generate NIST file" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rectangle"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:background="@drawable/rectangle"
android:layout_gravity="center_horizontal">
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="128dp"
android:text="Button" />
</LinearLayout>
</LinearLayout>
但是,如果我Button
在内部移动下方,LinearLayout
我的应用程序只会抛出异常:
LinearLayout cannot be cast to android.widget.Button
我觉得这很奇怪,我怎么能做到呢?
编辑:
这是我将按钮“导入”到我的 java 代码的方式:
setTabBar(R.layout.horz_scroll_app);
nist = (Button) findViewById(R.id.nist);
setTabBar
方法:
public void setTabBar(int id) {
LayoutInflater inflater = LayoutInflater.from(this);
scrollView = (MyHorizontalScrollView) inflater.inflate(
R.layout.horz_scroll_with_list_menu, null);
setContentView(scrollView);
menu = inflater.inflate(R.layout.horz_scroll_menu, null);
app = inflater.inflate(id, null);
ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);
ListView listView = (ListView) app.findViewById(R.id.list);
listView = (ListView) menu.findViewById(R.id.list);
ArrayList<MenuItem> menuItems = getMenuItems();
listView.setAdapter(new MenuCustomAdapter(this, menuItems));
btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);
btnSlide.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
btnSlide.setImageResource(R.drawable.lincolor);
break;
case MotionEvent.ACTION_UP:
btnSlide.setImageResource(R.drawable.lin);
break;
}
return false;
}
});
btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView,
menu));
final View[] children = new View[] { menu, app };
int scrollToViewIdx = 1;
scrollView.initViews(children, scrollToViewIdx,
new SizeCallbackForMenu(btnSlide));
}
我还应该提到,Button
如果我在内部添加其他元素,则此问题仅发生在元素上LinearLayout
。