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

问题描述 投票:6回答:4

我是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>

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

没有custom TextView.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);
    }

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"
         android:layout_marginTop="69dp"
         android:layout_toRightOf="@+id/textView1" />

</RelativeLayout>

Activity类sample.java ..

public class sample extends Activity{

    private static final String LOG_TAG = "sampleActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG,"OnCreate of sampleActivity");
        setContentView(R.layout.main);
}
}
android android-custom-view
4个回答
6
投票

我认为你在mycustomTextview中犯了一个小错误,当你夸大你的布局你也必须通过ViewGroup时,你可以使用这一行

mView = inflater.inflate(R.layout.cutomviewxml, this);

4
投票

要访问自定义视图内部的视图,请覆盖onFinishInflate()方法并使用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);
    }
}

3
投票

在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>

1
投票
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)
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.