I understand that if you inherit another layout when creating a custom view, you need to implement the creator below I wonder why the defStyleAttr and defStyleRes parameters are needed and used in what places.~ If possible, please let me know how to use it!
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
defStyleAttr is a parameter for specifying the basic properties of a view. If you look at the constructor code in EditText, you can see that it is defined as follows.
public class EditText extends TextView {
public EditText(Context context) {
this(context, null);
}
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
...
}
If you look at the second constructor, you can see that this() calls the third constructor. com to defStyleAttr.The properties defined in android.internal.R.attr.editTextStyle are given as follows:
<style name="Widget.EditText">
<item name="focusable">true</item>
<item name="focusableInTouchMode">true</item>
<item name="clickable">true</item>
<item name="background">?attr/editTextBackground</item>
<item name="textAppearance">?attr/textAppearanceMediumInverse</item>
<item name="textColor">?attr/editTextColor</item>
<item name="gravity">center_vertical</item>
<item name="breakStrategy">simple</item>
<item name="hyphenationFrequency">normal</item>
</style>
Although EditText inherits TextView, it would be easy to understand that EditText-specific attributes are set at the time of View creation through defStyleAttr. The difference between defStyleAttr and defStyleRes is the difference between R.attr.XXX and R.style.XXX.
Please refer to the link below for the relevant examples.
http://www.programering.com/a/MjNwATNwATg.html
599 GDB gets version error when attempting to debug with the Presense SDK (IDE)
887 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
568 Who developed the "avformat-59.dll" that comes with FFmpeg?
610 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.