我正在尝试在我的应用程序中实现自定义View。我浏览了这个页面: http://developer.android.com/training/custom-views/index.html
我创建了以下内容:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/leftMenu"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@drawable/left_menu_background"
android:orientation="vertical"
android:visibility="gone" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="52dp"
android:background="@drawable/left_menu_close_button"
android:onClick="showLeftMenu" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/left_menu_buttons_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/left_menu_data_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_button_transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="showSelectTab">
<ImageButton
android:id="@+id/left_menu_data_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="@drawable/folder"
android:onClick="showSelectTab"/>
<TextView
android:id="@+id/left_menu_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Data"
android:textColor="@color/my_white"
android:onClick="showSelectTab"/>
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
<LinearLayout
android:id="@+id/left_menu_settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="showSettingsPopup">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="@drawable/settings"
android:onClick="showSettingsPopup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Settings"
android:textColor="@color/my_white"
android:onClick="showSettingsPopup">
</TextView>
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
</LinearLayout>
</ScrollView>
和那些课程:
AppViewLinearLayout:
package com.emildesign.sgreportmanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
public class AppViewLinearLayout extends LinearLayout {
public AppViewLinearLayout(Context context, AttributeSet attrs, int layoutResource)
{
super(context, attrs);
if (isInEditMode())
return;
View view = LayoutInflater.from(context).inflate(layoutResource, null);
addView(view);
}
}
和: LeftSideMenu:
package com.emildesign.sgreportmanager.ui;
import com.emildesign.sgreportmanager.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class LeftSideMenu extends AppViewLinearLayout
{
private LeftSideMenuClickListener mListener;
public LeftSideMenu(Context context, AttributeSet attrs, int layoutResource)
{
super(context, attrs, layoutResource);
findViewById(R.id.left_menu_data).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.dataOnClick(v);
}
});
findViewById(R.id.left_menu_settings).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
}
public interface LeftSideMenuClickListener {
void dataOnClick(View v);
void settingsOnClick(View v);
}
}
现在我尝试将它添加到我的主要布局中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginScrActivity" >
<com.emildesign.sgreportmanager.ui.LeftSideMenu
android:id="@+id/leftSideMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
....
由于某种原因,我在logcat中收到了这个:
E/AndroidRuntime( 1034): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.emildesign.sgreportmanager/com.emildesign.sgreportmanager.activi
ties.ReportsTableActivity}: android.view.InflateException: Binary XML file line
#10: Error inflating class com.emildesign.sgreportmanager.ui.LeftSideMenu
更新
我在LeftSideMenu
中进行了以下更改:
public LeftSideMenu(Context context, AttributeSet attrs)
{
super(context, attrs, R.layout.left_menu);
.....
错误消失了,但我仍然可以看到自定义布局。
Hirarchy观众:
我究竟做错了什么? 任何帮助,将不胜感激。 感谢。
答案 0 :(得分:1)
添加此构造函数:
public LeftSideMenu (Context context, AttributeSet attrs) {...}