自定义属性

结构

 

<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 @   嘉禾世兴  阅读(266)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示