自定义属性

结构

 

<resources>
    <declare-styleable name="MyAttr">
        <attr name="myattr_title" format="string"></attr>
        <attr name="myattr_content" format="string"></attr>
    </declare-styleable>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"/>
    <TextView
        android:id="@+id/textview_content"
        android:layout_below="@id/textview_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容"/>
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
public class MyAttr extends RelativeLayout{
    private TextView textview_title,textview_content;
    public MyAttr(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyAttr);
        String title = typedArray.getString(R.styleable.MyAttr_myattr_title);
        String content = typedArray.getString(R.styleable.MyAttr_myattr_content);
        textview_title.setText(title);
        textview_content.setText(content);
    }

    private void init(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.myattr,this,true);
        textview_title = (TextView) view.findViewById(R.id.textview_title);
        textview_content = (TextView) view.findViewById(R.id.textview_content);
    }

    public void setTitle(String title){
        textview_title.setText(title);
    }

    public void setContent(String content){
        textview_content.setText(content);
    }
}
public class MainActivity extends AppCompatActivity {

    private MyAttr myAttr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myAttr = (MyAttr) findViewById(R.id.myattr);
        myAttr.setTitle("我是java定义的标题");
        myAttr.setContent("我是java定义的内容");
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myttr="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.mydefineattr.MainActivity">
    <com.example.administrator.mydefineattr.MyAttr
        android:id="@+id/myattr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myttr:myattr_title="我是xml定义的标题"
        myttr:myattr_content="我是xml定义的内容"></com.example.administrator.mydefineattr.MyAttr>
</RelativeLayout> 

 

posted @ 2016-04-21 16:59  嘉禾世兴  阅读(265)  评论(0编辑  收藏  举报