Android 初始化一个类或活动中不同活动中使用的公共UI元素

Android 初始化一个类或活动中不同活动中使用的公共UI元素,android,android-layout,Android,Android Layout,我有几个EditText和TextView,它们在我的应用程序中跨不同的布局和活动使用,并且必须在每个类中初始化它们。我在布局中使用了标记。 我想找的是,我能不能有一个类,在这个类中我可以初始化所有这些公共视图,设置一些所需的属性,并在所有其他类中使用此方法。cratet new class CustomTextView.java public class CustomTextView extends TextView { Context mContext; public C

我有几个EditText和TextView,它们在我的应用程序中跨不同的布局和活动使用,并且必须在每个类中初始化它们。我在布局中使用了
标记。
我想找的是,我能不能有一个类,在这个类中我可以初始化所有这些公共视图,设置一些所需的属性,并在所有其他类中使用此方法。

cratet new class CustomTextView.java

public class CustomTextView extends TextView {

    Context mContext;

    public CustomTextView(Context context,AttributeSet attrs){
        super(context,attrs);
        init(context);
    }
    public CustomTextView(Context context) {
        super(context);
        init(context);  
    }

    private void init(Context context){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(inflater != null){       
            LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.custom_text_view, new LinearLayout(context));

            mContext = context;


            // set any TextView attribute here...
            this.setText("hello!!!");
        }
    }
}
然后调用新的CustomTextView(this),或者将其包含在xml中

自定义_text_view.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >    
</TextView> 


您可以将视图扩展到自定义视图,并在扩展类中初始化它们。我得到的答案是,我必须创建一个扩展活动的类,然后创建一个扩展该类的类?不,将TextView和EditText扩展到CustomTextView和CustomEditText,将它们膨胀,并初始化它们的参数。我用xml声明此视图,然后在android中使用它们,在运行时设置值并更改字体。但由于此视图在许多类中重复,因此代码在每个类中都重复。我想在一个类中编写一次这段代码,然后在其他每个类中再次使用这段代码。