开发者

How to optimize 'class name' in Android XML Layout?

I am using some custom components in my project for that I am using following code.

 <view
  class="com.android.mypackage.myclass" 
开发者_运维问答  id="@+id/button"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@android:drawable/button"
  android:padding="10dip"
  />

Its working perfectly fine, I am using this code near about 35 times in my application. so while creating new clone application from same project, I need to update package name in 35 places. Is there any way to reduce these efforts? I had tried with "class="@string/class_name" but its not working.


Don't put your custom views in your original package, and just put them in another package.
You can also put them as following pic.

How to optimize 'class name' in Android XML Layout?



And the code in layout xml is like

<?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"
 >
<MyView
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/>
</LinearLayout>

It is more convenient.


No. You only have the alternative to write:

<com.android.mypackage.myclass
    id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:drawable/button"
    android:padding="10dip" />

class="@string/class_name does not work, because the name of the class must be constant and be available at compile time while generating Java code from XML.

@string/class_name means that the value of the field R.string.class_name is "dynamically" read while runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜