Android custom layout attribute "reference attribute"?

I have a custom view (which extends the view group) and I specified some user attributes defined in attrs.xml ....

<declare-styleable name="datascope"> <attr name="colcount" format="integer" /> <attr name="titleheaderrows" format="integer" /> <attr name="colheaderrows" format="integer" /> <attr name="rowlayout" format="reference" /> </declare-styleable> 

An integer that I can choose, but the last one is rowlayout - I want to use it to link to another layout file, which I will inflate on demand. But I cannot find the correct way to express the rowlayout attribute in the main layout file. I tried:

 lui:rowlayout="@layout/sensorvaluesdata"> 

but this does not execute at runtime:

E / AndroidRuntime (22092): caused by: java.lang.NumberFormatException: cannot parse 'res / layout / sensorvaluesdata.xml' as integer

and

 lui:rowlayout="?layout/sensorvaluesdata" 

which fails

E / AndroidRuntime (22341): caused by: java.lang.NumberFormatException: cannot parse '? 2130903043 'as a whole

This is interesting because it seems to be stuck in a resource identifier, but comes to the fore ? .

My R.java files have a reasonable looking line for sensorvaluesdata .

 public static final class layout { public static final int sensorvaluesdata=0x7f030003; } 

What is the right way to do this?

(I can write code to a java source and it works fine.

 View vx = li.inflate(R.layout.sensorvaluesdata, this, false); 
+6
source share
1 answer

Just add a little more background to someone else. In code use something like this

 LinearLayout ViewContainer=(LinearLayout) (LayoutInflater.from(context)).inflate( attributes.getResourceId( R.styleable.[styleableName]_[attributeName], R.layout.[defaultValue]), null); 

In the xml attributes under your name ...

 <attr name="layout" format="reference"/> 
+5
source

Source: https://habr.com/ru/post/909768/


All Articles