以编程方式设置自定义xml布局元素

时间:2011-09-21 01:24:09

标签: java android custom-view

在我的代码中,我有一个类标题'CustomView',它有两个TextView字段并扩展了相对布局。一个实例化CustomView使用xml布局对其进行自我膨胀,称为“custom_view_layout”,它显示两个文本字段,同样在实例化时,这两个文本视图等同于CustomView的文本视图。

在我的'main'xml布局文件中,我有一个'CustomView'的自定义标签,你可以从xml看到这个标签有一些布局参数。我想要的是以编程方式实例化CustomView,填充它的文本视图,然后将我的主xml布局文件中的自定义视图“设置”到此实例化视图。我喜欢以比将cusomview标签布局参数传递给我的CustomView实例更光滑的方式执行此操作,然后将自定义视图标签替换为自定义视图的实例。

在我的代码中,我已经注释了两次失败的尝试。任何建议都将非常受欢迎。

自定义视图类:

package com.customviewsetexample;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomView extends RelativeLayout {

    TextView textView1;
    TextView textView2;

    public CustomView(Context context) {
        super(context);
        initView(context);
        this.textView1 = (TextView)this.findViewById(R.id.textView1);
        this.textView2 = (TextView)this.findViewById(R.id.textView2);

    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        this.textView1 = (TextView)this.findViewById(R.id.textView1);
        this.textView2 = (TextView)this.findViewById(R.id.textView2);
    }

    private void initView(Context context){
        this.inflate(context, R.layout.custom_view_layout, this);
    }

    public void populateCustomView(String string_1, String string_2){
        System.out.println("Started");
        this.textView1.setText(string_1);
        System.out.println("mid");
        this.textView2.setText(string_2);
        System.out.println("end");
    }

}

我的两次尝试的活动课程注释掉了:

package com.customviewsetexample;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomViewSetExampleActivity extends Activity {
    /** Called when the activity is first created. */

    String string_1;
    String string_2;
    CustomView customView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String string_1 = "String 1";
        String string_2 = "String 2";
        customView = new CustomView(this);
        customView.populateCustomView(string_1, string_2);
        CustomView mainCustomView = (CustomView)findViewById(R.id.mainCustomView);

        /*
         //This Does nothing
        mainCustomView = customView;
         */

        /*
       //Throws Error see below
       mainCustomView.updateViewLayout(customView, mainCustomView.getLayoutParams());
        */
    }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<com.customviewsetexample.CustomView     xmlns:android="http://schemas.android.com/apk/res/android"       
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainCustomView"
    >

custom_view_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:text="TextView" android:layout_height="wrap_content"    android:id="@+id/textView1" android:layout_width="wrap_content"    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></TextView>
    <TextView android:text="TextView" android:layout_height="wrap_content"     android:id="@+id/textView2" android:layout_width="wrap_content"    android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView1"></TextView>

更新查看尝试的错误消息:

09-21 01:02:40.121: ERROR/AndroidRuntime(1592): Uncaught handler: thread main exiting due to uncaught exception
09-21 01:02:40.130: ERROR/AndroidRuntime(1592): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customviewsetexample/com.customviewsetexample.CustomViewSetExampleActivity}: java.lang.IllegalArgumentException: Invalid LayoutParams supplied to com.customviewsetexample.CustomView@43762b20
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.os.Looper.loop(Looper.java:123)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at java.lang.reflect.Method.invoke(Method.java:521)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at dalvik.system.NativeStart.main(Native Method)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592): Caused by: java.lang.IllegalArgumentException: Invalid LayoutParams supplied to com.customviewsetexample.CustomView@43762b20
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1759)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at com.customviewsetexample.CustomViewSetExampleActivity.onCreate(CustomViewSetExampleActivity.java:34)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-21 01:02:40.130: ERROR/AndroidRuntime(1592):     ... 11 more

1 个答案:

答案 0 :(得分:0)

在您试图给视图充气的CustomView课程中,首先需要注册布局充气服务。

将正确的上下文传递给类的构造函数,然后使用您传递的上下文注册进行膨胀,然后进行膨胀。

尝试类似:

      private void initView(Context context){
        LayoutInflater infl = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View temp = infl.inflate(R.layout.test, null); //contains your entire xml ofcourse
        }

当您尝试将自定义视图设置为主xml时,您可以快速执行此操作,例如对主xml进行充气,然后使用.addView(customview)您可以在setContentView()之前执行此操作如果不符合您的要求或流程。

这里有很多东西需要重新修改。