Good day.

There is an inheritor of the view class, custom buttons are drawn on it on the outline.
How to connect it in xml markup?

Update

Well, I tried:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <com.planningtask.planningtime.ControllerTime/> </LinearLayout> 

Received:

11-16 21: 38: 07.680: E / AndroidRuntime (30791): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.planningtask.planningtime / com.planningtask.planningtime.StartLaunch}: android.view.InflateException: Binary XML file line # 7: Error inflating class com.planningtask.planningtime.ControllerTime

  • For example, the class MyControllerView is in the ru.my.app directory, then in xml it will look like this: <ru.my.app.MyControllerView android: layout ... </ru.my.app.MyControllerView> - Helisia
  • Received: 11-16 21: 38: 07.680: E / AndroidRuntime (30791): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.planningtask.planningtime / com.planningtask.planningtime.StartLaunch}: android.view.InflateException : Binary XML file line # 7: Error inflating class com.planningtask.planningtime.ControllerTime - alexmx
  • one
    Add constructors (Context c), (Context c, AttributeSet as), (Context c, AttributeSet as, int def) to the class and transfer the initialization to them. - Helisia
  • Thank you, but I don’t understand what to initialize in them - alexmx
  • one
    @alexmx, under initialization, I meant the code that you (probably) have in your constructor. Now I will draw everything up and transfer it back. - Helisia Nov.

1 answer 1

For example, the class MyControllerView is in the ru.my.app directory, then in xml it will look like this:

 <ru.my.app.MyControllerView <!-- здесь расписываете все нужные атрибуты --> </ru.my.app.MyControllerView> 

If you inherit from View and want to connect an element from the xml markup, then in addition to the current constructor (for example, (Context mContext) ), you must do at least one more OR replace it:

 public название_класса(Context con, AttributeSet set) { //Вот сюда переносим весь код из //Старого конструктора, а его убираем } 

The remaining constructors are used rarely and extremely rarely, respectively:

 public название_класса(Context con) {} public название_класса(Context con, AttributeSet set, int ds) {} 

Update

Yes, otherwise get another departure. The minimum attribute set is android:layout_width and android:layout_height .

  • All understood, all chewed))), and the attributes are required? - alexmx
  • @alexmx, Updated the answer - Helisia
  • Thanks)) - alexmx
  • But I did not understand where in the class is written android: layout_width? - Andro