[英]Set tag abbreviations for custom views on XML
In my app I extend LinearLayout
and RelativeLayout
, so every time I need to declare a layout in XML (which you may know it's quite often) I need to write a long tag , in order to point to the view's package. 在我的应用程序中, 我扩展了
LinearLayout
和RelativeLayout
,因此每次我需要用XML声明一个布局(您可能经常知道)时,我都需要写一个long标记 ,以指向视图的包。
So the XML files look like this: 因此,XML文件如下所示:
<com.company.material.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.company.material.widget.RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/titlebar_height"
android:background="@color/primary"
android:orientation="horizontal">
<TextView
style="@style/Text.Field.Small"
android:id="@+id/form_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
style="@style/Button.Main"
android:id="@+id/form_submit"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</com.company.material.widget.RelativeLayout>
<com.company.essentials.view.FormView
android:id="@+id/formview"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
This is chaotic! 这太混乱了!
Is there any way to abbreviate this? 有什么办法可以简化吗? Like
AppLinearLayout
instead of com.company.material.widget.LinearLayout
? 喜欢
AppLinearLayout
而不是com.company.material.widget.LinearLayout
吗?
Thanks in advance! 提前致谢!
If it's worth it to you, you can customize your activity's layout inflater. 如果值得,您可以自定义活动的布局填充器。
public class MyActivity extends Activity implements LayoutInflater.Factory {
@Override public void onCreate(Bundle _icicle) {
super.onCreate(_icicle);
setContentView(R.layout.my_activity);
}
@Override public Object getSystemService(String _name) {
Object service = super.getSystemService(_name);
if(LAYOUT_INFLATER_SERVICE.equals(_name)) {
LayoutInflater myInflater = ((LayoutInflater)service).cloneInContext(this);
myInflater.setFactory(this);
return myInflater;
}
return service;
}
@Override public View onCreateView (String _tag, Context _ctx, AttributeSet _as) {
if("mytag".equals(_tag))
return new MyLinearLayout(_ctx, _as);
else
return null;
}
}
setContentView
will call getSystemService
to obtain a layout inflater, and for each tag that layout inflater will query each of its factories (including our custom one) to see if the factory knows how to create the object that corresponds to that tag. setContentView
将调用getSystemService
以获得一个布局填充器,并且对于该布局填充器的每个标签,它将查询其每个工厂(包括我们的自定义工厂),以查看工厂是否知道如何创建与该标签相对应的对象。
You could move your View class to the android.view
package. 您可以将View类移至
android.view
包。 This would allow you to just write <AppRelativeLayout />
instead of <com.company.material.widget.AppRelativeLayout />
, since View tag names without package prefixes are 'auto-completed' to the android.view
package. 这将允许您只编写
<AppRelativeLayout />
而不是<com.company.material.widget.AppRelativeLayout />
,因为没有包前缀的View标签名称是“自动完成的”到android.view
包。
If you don't want to move your whole class to this package, you may just create a dummy sub-class à la: 如果您不想将整个类移至此包,则可以只创建一个虚拟子类:
package android.view;
public class AppRelativeLayout extends com.company.material.widget.RelativeLayout {
// same constructors as super class
public AppRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
You just have to make sure you don't use the same class names as the Android framework already does, such as RelativeLayout
, since that would clash with the existing views and layout names. 您只需要确保您没有使用与Android框架相同的类名,例如
RelativeLayout
,因为这将与现有的视图和布局名冲突。 That's why I named the example above AppRelativeLayout
. 这就是为什么我将示例命名为
AppRelativeLayout
。
There is alternative option as well if you like you can do it like this 如果您愿意,也可以有其他选择
<view
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.company.material.widget.LinearLayout"
android:orientation="vertical">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.