开发者

Where the layout reads the resources?

开发者 https://www.devze.com 2023-03-25 03:58 出处:网络
Which method i must override, that the xml layout use to read the resources linked with, sample: if i had TextView within my layout:

Which method i must override, that the xml layout use to read the resources linked with, sample:

if i had TextView within my layout:

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/textview1"
                android:text="@String/text_开发者_Python百科test1"
                </TextView>

and in my Activity in onCreate, we always use setContentView: setContentView(R.layout.sample);

--

my question is : when we use xml file as layout in activities, how the layout reads the resources like android:text="@String/text_test1, so i can for example override it, so when the layout request any String i can bind another text to it ?

--

Note : am not looking for regular way, by finding the view in the XML, and then change it properties. am looking for abstract way to change every String requested in the resources by xml layout.


To change the text you need to simply use this in your activity.

  ((TextView)findViewById(R.id.textview1)).setText("New Text");

To override the entire TextView you need to create a custom textview class that extends TextView and perform your stuff there.


if you want to change the text by code use this:

TextView textView = (TextView) findViewById(R.id.textview1);

textView.setText(getResources().getString(R.string.yourtextishere));


I'm not sure about what you mean exatly. Anyway in the res/ folder there are all the resources that your application will need at runtime. A String is a resource defined in the value/string.xml file.

At complie time the ADT generate a java static class that bind an ID for any of your resources, strings too. So when you set the layout of your activity the vaule of the string will be get by the specific ID in that class.

if you ment to change the textView text you'll just have to get the object rappresenting the textView at runtime with:

TextView textView = (TextView) findViewById(R.id.textview1);

and the use his methods to set the text:

textView.setText("New Text");

hope this will help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号