Can I implement some logic in XML?
开发者_StackOverflow社区I need XML for creating layouts in my Android apps and what I wanted to know is the following. Can I implement some logic in XML? For instance, I wanted to position my text exactly "(fill_parent - (the width of the image))/2"... something like this.
you cant do that directly in xml file. if u want to position ur text exactly means just set the positons of the text, check properities of text.
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.textView1);
main.xml
contains textview with id textview1. like this u can acess the textview of ur xml file..
Not inside the XML. The usual way this is done is to locate the views with findViewById()
in onCreate()
after the setContentView()
, then adjust the relevant properties programmatically.
Edit: after re-reading the example in the question, my advice seems too general (towards modifying any properties on a view). For calculated layout positions you are better achieving these kinds of results with relative layouts or some of the specialized view containers.
精彩评论