Android 扩展视图类并使用布局xml文件的示例代码
我是Android世界的新手。。。。如果有人纠正我,这将是一个很大的帮助。。。我做错了什么下面的代码Android 扩展视图类并使用布局xml文件的示例代码,android,android-custom-view,Android,Android Custom View,我是Android世界的新手。。。。如果有人纠正我,这将是一个很大的帮助。。。我做错了什么下面的代码 要求:需要创建自定义视图(使用xml布局文件),以便在我的应用程序活动中使用相同的视图。下面是我正在编写的示例代码 cutomviewxml.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 要求:需要创建自定义视图(使用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="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
活动main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.motorola.mycustomTextview
android:id="@+id/customtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginLeft="143dp"
android:layout_marginTop="69dp"
android:layout_toRightOf="@+id/textView1" />
</RelativeLayout>
在xml中使用此选项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<yourpackagename.viewclassname android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></yourpackagename.viewclassname>
</LinearLayout>
要访问自定义视图覆盖onfinishflate()方法内部的视图,请使用findViewById()。对于手头的问题,解决方法如下
public class mycustomTextview extends LinearLayout {
Context mycontext;
private TextView textView1;
public mycustomTextview(Context context, AttributeSet attrs) {
super(context, attrs);
this.mycontext = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
textView1=(TextView) findViewById(R.id.textView1);
}
}
我认为您在
mycustomTextview
中犯了一个小错误,当您膨胀布局时,您也必须通过ViewGroup,您可以使用这一行
mView = inflater.inflate(R.layout.cutomviewxml, this);
如果必须,如何将参数传递给mycustomTextview类?
public class mycustomTextview extends LinearLayout {
Context mycontext;
private TextView textView1;
public mycustomTextview(Context context, AttributeSet attrs) {
super(context, attrs);
this.mycontext = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
textView1=(TextView) findViewById(R.id.textView1);
}
}
mView = inflater.inflate(R.layout.cutomviewxml, this);
public class ExampleView extends FrameLayout {
ImageView mImage;
public ExampleView(Context context) {
super(context);
init(context);
}
public void init(Context context) {
View view = inflate(context, R.layout.my_view_layout,this);
view.findViewById(R.id.image)
}
}