How to optimize "class name" in Android XML layout?

I use some custom components in my project because I use the 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" /> 

Works fine, I use this code about 35 times in my application. therefore, when creating a new clone application from the same project, I need to update the package name in 35 places. Is there a way to reduce this effort? I tried with "class =" @ string / class_name "but did not work.

+4
source share
2 answers

Do not put your custom views in the source package and just put them in another package.
You can also place them as the following pic.
alt text

And the code in the xml layout 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.

+1
source

You only have the opportunity 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 class name must be constant and be available at compile time when generating Java code from XML.

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

0
source

Source: https://habr.com/ru/post/1333554/


All Articles