Reading Android attributes on my user view

I created my own layout class (extends RelativeLayout) and has a TextView as part of the layout.

I want to apply the properties declared in XML to my TextView, anyway I can read the android attributes (and not my user attributes, this part has already taken care).

For example, in my XML, I will have this:

<my.custom.MyLayout
    android:layout_width="100dp"
    android:layout_height="20dp"
    android:text="SomeText" /> 

I want to read a text attribute and apply it to my TextView (it is currently applied to RelativeLayout) instead of creating my own attribute and reading it.

My custom layout looks something like this:

public class MyLayout extends RelativeLayout {

    private TextView textView;
    public void MyLayout(final Context context, final AttributeSet attrs) {
        /**Read android attributes and apply it to TextView **/
        ??
    }

My current solution is to create custom attributes and read them, but I think this is not a good solution, as I will duplicate every attribute declared in TextView.

.

myText, , XML, TextView.

XML:

myNameSpace:myText="SomeText"

Java:

String text= a.getString(R.styleable.MyStyleable_myText);
textView.setText(text);

"android:".

+4
1

, , android:text RelativeLayout. , TextView:

final Resources.Theme theme = context.getTheme();
a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
text = a.getText(attr);

, , , .

, . . TextView , XML, .

<YourCustomView>
    <TextView android:text="someText"/>
<YourCustomView/>
+6

Source: https://habr.com/ru/post/1611878/


All Articles