在Android中以编程方式创建视图时如何传递AttributeSet [英] How to pass AttributeSet when creating view programmatically in android

查看:127
本文介绍了在Android中以编程方式创建视图时如何传递AttributeSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然后像水平视图一样以编程方式创建,如何以编程方式传递AttributeSet.

I create programmatically like horizontalview then, how to pass AttributeSet in programmatically.

我的构造函数如下:

public HorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

我已经尝试过:

mHlvSimpleList= new HorizontalListView(mcontext,R.style.niceview);

错误:

未定义构造函数Horizo​​ntalListView(Context,int)

The constructor HorizontalListView(Context, int) is undefined

in style.xml

in style.xml

<style name="niceview">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>

</style>

如何在horizo​​ntalistview构造函数匹配参数中传递AttributeSet?

How to pass AttributeSet in horizontalistview constructor matching parameter?

推荐答案

从XML扩展视图时,将使用具有ContextAttributeSet的构造函数.您不应该使用它来创建对象.您应该使用带有Context作为参数的构造函数.

The constructor with Context and AttributeSet is used when your view is inflated from xml. You shouldn't use it to create object. You should use constructor with Context as param.

AttributeSet是接口,您可以创建then的实例并实现所有方法,如下所示:

AttributeSet is interface and you can create instance of then and implement all method as is shown below:

AttributeSet attrs = new AttributeSet(){
        @Override
        public int getAttributeCount() {
            return 0;
        }

        @Override
        public String getAttributeName(int index) {
            return null;
        }

        @Override
        public String getAttributeValue(int index) {
            return null;
        }

        @Override
        public String getAttributeValue(String namespace, String name) {
            return null;
        }

        @Override
        public String getPositionDescription() {
            return null;
        }

        @Override
        public int getAttributeNameResource(int index) {
            return 0;
        }

        @Override
        public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
            return 0;
        }

        @Override
        public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) {
            return false;
        }

        @Override
        public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
            return 0;
        }

        @Override
        public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
            return 0;
        }

        @Override
        public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) {
            return 0;
        }

        @Override
        public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) {
            return 0;
        }

        @Override
        public int getAttributeListValue(int index, String[] options, int defaultValue) {
            return 0;
        }

        @Override
        public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
            return false;
        }

        @Override
        public int getAttributeResourceValue(int index, int defaultValue) {
            return 0;
        }

        @Override
        public int getAttributeIntValue(int index, int defaultValue) {
            return 0;
        }

        @Override
        public int getAttributeUnsignedIntValue(int index, int defaultValue) {
            return 0;
        }

        @Override
        public float getAttributeFloatValue(int index, float defaultValue) {
            return 0;
        }

        @Override
        public String getIdAttribute() {
            return null;
        }

        @Override
        public String getClassAttribute() {
            return null;
        }

        @Override
        public int getIdAttributeResourceValue(int defaultValue) {
            return 0;
        }

        @Override
        public int getStyleAttribute() {
            return 0;
        }
    }; 

并使用它

TextView textView = new TextView(this, attrs);

但是这不是正确的方式.

您应该使用视图中的方法来设置视图属性.

例如,设置LayoutParams是执行此操作的两种方法

For example to set LayoutParams is two way to do this

首先通过方法setLayoutParams()

view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

将视图添加到ViewGroup中的第二个;

The second when you added your view to ViewGroup;

viewGroup.addView(yourView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

在拥有视图或将视图添加到例如RelativeLayout时,应使用与此视图组相关的LayoutParams.是RelativeLayout.LayoutParams

When you have or you want to add your view to for example to RelativeLayout you should use LayoutParams relevant for this ViewGroup. It is RelativeLayout.LayoutParams

这篇关于在Android中以编程方式创建视图时如何传递AttributeSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆