小编Sat*_*apu的帖子

扩展View类和使用layout xml文件的示例代码

我是Android世界的新手......如果有人纠正我,那将是一个很好的帮助...我在做错了以下代码...

  • 要求:需要创建自定义视图(使用xml布局文件),以便在我的应用程序活动中使用相同的视图.在这里,我使用我正在处理的示例代码,

cutomviewxml.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>
Run Code Online (Sandbox Code Playgroud)

扩展视图类...代码......

mycustomTextview.java

public class mycustomTextview extends View {

    private View mView;
    Context mycontext;

    public mycustomTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

    this.mycontext = context;

    LayoutInflater inflater;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mView = inflater.inflate(R.layout.cutomviewxml, null);
    }
Run Code Online (Sandbox Code Playgroud)

Activity 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" …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

android-custom-view ×1