How to pass AttributeSet when creating view programmatically in android

非 Y 不嫁゛ 提交于 2019-11-30 07:19:13

问题


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

My constructor looks like this:

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

I have try this:

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

Error:

The constructor HorizontalListView(Context, int) is undefined

in style.xml

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

</style>

How to pass AttributeSet in horizontalistview constructor matching parameter?


回答1:


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 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;
        }
    }; 

And use it

TextView textView = new TextView(this, attrs);

but it is not correct way.

You should use methods from your view to set properties of view.

For example to set LayoutParams is two way to do this

First by method setLayoutParams()

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

The second when you added your view to ViewGroup;

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

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




回答2:


i have a way to get the AttributeSet from a xml file.

XmlPullParser parser = getResources().getXml(R.xml.test);
        try {
            parser.next();
            parser.nextTag();
        } catch (Exception e) {
            e.printStackTrace();
        }

        AttributeSet attr = Xml.asAttributeSet(parser);
        int count = attr.getAttributeCount();

if the count value is bigger than 0, then means you get a right AttributeSet.

my xml/test.xml is like these:

<?xml version="1.0" encoding="utf-8"?>
<com.galoisli.myapplication.MyListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />



回答3:


I ran into this question while working with a view that forgot to implement the SomeView(Context) constructor and only had the one with SomeView(Context, AttributeSet) available. From the source code of ViewGroup it appears that SomeView(context) would be equivalent to SomeView(context, null), though.




回答4:


You can create an attribute referencing a custom style. Then you should be able to instantiate it using

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

A full example is available there https://gist.github.com/romannurik/7026222



来源:https://stackoverflow.com/questions/29028168/how-to-pass-attributeset-when-creating-view-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!