مُنشئ عرض Android Custom
-
04-10-2019 - |
سؤال
أنا أتعلم استخدام طرق عرض مخصصة من ما يلي:
http://developer.android.com/guide/topics/ui/Custom-Components.html#modify
الوصف يقول:
تهيئة الفصل كما هو الحال دائمًا ، يسمى السوبر أولاً. علاوة على ذلك ، هذا ليس مُنشئًا افتراضيًا ، ولكنه معلم. يتم إنشاء edittext باستخدام هذه المعلمات عندما يتم تضخيمها من ملف تخطيط XML ، وبالتالي ، يحتاج مُنشئنا إلى أخذها وتمريرها إلى مُنشئ الطبقة الفائقة أيضًا.
هل هناك وصف أفضل؟ لقد كنت أحاول معرفة الشكل الذي يجب أن يبدو عليه المُنشئ (S) وتوصلت إلى 4 خيارات ممكنة (انظر مثال في نهاية المنشور). لست متأكدًا مما تفعله هذه الخيارات الأربعة (أو لا تفعل ذلك) ، ولماذا يجب أن أقوم بتنفيذها ، أو ما تعنيه المعلمات. هل هناك وصف لهذه؟
public MyCustomView()
{
super();
}
public MyCustomView(Context context)
{
super(context);
}
public MyCustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyCustomView(Context context, AttributeSet attrs, Map params)
{
super(context, attrs, params);
}
المحلول
لا تحتاج إلى الأول ، لأن ذلك لن يعمل.
الثالث سيعني العرف الخاص بك View
سيكون قابلاً للاستخدام من ملفات تخطيط XML. إذا كنت لا تهتم بذلك ، فلن تحتاجه.
الرابع هو مجرد خطأ ، afaik. لا يوجد View
مُنشئ يأخذ Map
كمعلمة ثالثة. هناك واحد يأخذ int
كمعلمة ثالثة ، تستخدم لتجاوز النمط الافتراضي للعناصر واجهة المستخدم.
أنا أميل إلى استخدام this()
بناء الجملة للجمع بين هذه:
public ColorMixer(Context context) {
this(context, null);
}
public ColorMixer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ColorMixer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// real work here
}
يمكنك رؤية بقية هذا الرمز في مثال هذا الكتاب.
نصائح أخرى
إليك نمطي (إنشاء مخصص ViewGoup
هنا ، ولكن لا يزال):
// CustomView.java
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
init(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
LayoutInflater.from(ctx).inflate(R.layout.view_custom, this, true);
// extra init
}
}
و
// view_custom.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Views -->
</merge>
عندما تضيف العرف الخاص بك View
من xml
مثل :
<com.mypack.MyView
...
/>
ستحتاج إلى مُنشئ عام MyView(Context context, AttributeSet attrs),
وإلا ستحصل على Exception
متى Android
يحاول أن inflate
لك View
.
وعندما تضيف View
من xml
و أيضا حدد ال android:style
attribute
مثل :
<com.mypack.MyView
style="@styles/MyCustomStyle"
...
/>
ستحتاج أيضًا إلى الثالث مُنشئ عام MyView(Context context, AttributeSet attrs,int defStyle)
.
عادة ما يتم استخدام المنشئ الثالث عند تمديد النمط وتخصيصه ، ثم ترغب في تعيين ذلك style
إلى معين View
في تخطيطاتك
عدل التفاصيل
public MyView(Context context, AttributeSet attrs) {
//Called by Android if <com.mypack.MyView/> is in layout xml file without style attribute.
//So we need to call MyView(Context context, AttributeSet attrs, int defStyle)
// with R.attr.customViewStyle. Thus R.attr.customViewStyle is default style for MyView.
this(context, attrs, R.attr.customViewStyle);
}