stackoverflow转载(关于对attributeSet理解)

how to define AttributeSet parameter for extended layout

up vote 1 down vote favorite

I am having trouble understanding how to instantiate a custom layout in code. Specifically I do not know how to define the requiredAttributeSet parameter.

I have gone through the official docs but could not wrap my head around it :

http://developer.android.com/training/custom-views/create-view.htmlhttp://developer.android.com/reference/android/util/AttributeSet.html

In my activity, i instantiate it with :

new MyCustomLayout(getActivity(), attrs)

But how do i define attrs?

MyCustomLayout.java

public class MyCustomLayout extends LinearLayout implements
        OnClickListener {

    ...
    ...

    public MyCustomLayout(Context context, AttributeSet attrs) {

        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.mycustomxmllayout, this, true);
        ...
    }

    ...
}

mycustomxmllayout.xml

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android" >

     
        android:id="@+id/txt_1"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" 
        android:lines="1"/>

     
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:background="@null"
        android:text="x"
        android:layout_weight="1"
        />

share edit
 

1 Answer

active oldest votes
up vote 1 down vote accepted

Firstly, you should define your custom attributes in the res/values/attrs.xml, just add, for example:

    name="MyCustomLayout">
        name="showText" format="boolean" />
        name="buttonPosition" format="enum">
            name="left" value="0"/>
            name="right" value="1"/>
       
    

Now you can set your custom attrs via xml, like this:

 xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res-auto">
 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     custom:showText="true"
     custom:buttonPosition="left" />

And in your constructor you can grab that attrs:

public MyCustomLayout(Context context, AttributeSet attrs) {
   super(context, attrs);
   TypedArray a = context.getTheme().obtainStyledAttributes(
        attrs,
        R.styleable.MyCustomLayout,
        0, 0);

   try {
       mShowText = a.getBoolean(R.styleable.MyCustomLayout_showText, false);
       mButtonPos = a.getInteger(R.styleable.MyCustomLayout_buttonPosition, 0);
   } finally {
       a.recycle();
   }
}

If you want to set your attrs programmatically, you should create public getters/setters for that:

private boolean mShowText;
private Integer mButtonPos;

public void setButtonPos(int pos) {
    mButtonPos = pos;
}

public void setShowText(boolean showText) {
    mShowText = showText;
}

After that you can set your attrs programmatically:

MyCustomLayout layout = new MyCustomLayout(getActivity());
layout.setButtonPos(0);
layout.setShowText(true);
share edit
 
 
that's interesting. I was hoping to simply reuse the existing attributes of the framework's LinearLayout. That is why i extended it. I do not want to defined any custom attributes. Do you think that is possible? –  faizal Nov 1 '14 at 7:03
 
What attrs do you want to use, for example? And where do you want to use it? –  rom4ek Nov 1 '14 at 7:06
 
I just need theAttributeSet parameter to instantiate the layout in my activity i.e. the second parameter innew MyCustomLayout(getActivity(), attrs). So whatever is the minimum required to get it working is all i am looking for. I am guessing this meanslayout_height and layout_width. If i can skip the parameter all together, it would be even better. –  faizal Nov 1 '14 at 7:10
1  
The constructor that haveattrs parameter is only used when you create your layout from xml, not programmatically. If you create your layout programmatically you should also set it properties programmatically. –  rom4ek Nov 1 '14 at 7:12
 
So instead of usingsuper(context, attrs) in my constructor, are you saying i can simply usesuper(context) and then set the required attributes using setLayoutParams() ? –  faizal Nov 1 '14 at 7:24



事实上理解 AttributeSet就要明白:
     1:how do i get attribute sets in the activity to pass to my custom Class
      2:一般的: The AttributeSet Constructrs is used when a view is inflated through a layout define in XML  . if you are constructing one in code ,you should use the single argument construct
    3: 但是 如果你一定使用 no-single argument constructor 那么你可以按照上面(stackOverflow)的写法来做   就是在 custom class 添加setter/getter方法

你可能感兴趣的:(stackoverflow转载(关于对attributeSet理解))