<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><strong><span style="font-family:Microsoft YaHei;">废话叫前言</span></strong></span>
开发语言的进步,感触最新的就是,以前写windows的时候使用的是C#后来,学习了一下WPF,内心就是感慨万千
居然用xml来写界面了,不用那些抽象的代码来写,关键是xml的界面还用实时的可观性。xml就和html一样,标记语言。
可以直接在浏览器看到。
之后学习Android后,感觉又回来,真的,语言在进步呀。
在Android中xml时如何连接到java的呢?
是这样的:
value中新建一个atrrs.xml
      <?xml version="1.0" encoding="utf-8"?>
     <resources>
         <declare-styleable name="DivActionBar">
               <attr name="right_onclick" format="string" />
          </declare-styleable>
    </resources>
xml中引用:
使用空间名:
xmlns:app=”http://schemas.android.com/apk/res/com.tuxiaobei“
使用
app:right_onclick="function"
java中使用:
       <strong><span style="font-size:18px;">TypedArray array = context
                .obtainStyledAttributes(attrs, R.styleable.DivActionBar);
        final String methodName;</span>
        <span style="font-size:18px;">methodName = array.getString(R.styleable.DivActionBar_right_onclick);</span></strong>
        rightText.setOnClickListener(new OnClickListener() {
            private Method method;
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(methodName)) {
                    if (method == null) {
                        try {
                            method = getContext().getClass().getMethod(methodName,
                                    DivActionBar.class);
                        } catch (NoSuchMethodException e) {
                            throw new IllegalStateException("Could not find a method "
                                    + method + "(View) in the activity "
                                    + getContext().getClass() + " for onClick handler"
                                    + " on view " + DivActionBar.this.getClass() + e);
                        }
                        try {
                            method.invoke(getContext(), DivActionBar.this);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>