开发者

How to get a view from a resource?

I have a UI, I build it dynamically. I should want to put some component in a xml resource file. So I do :

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+android:id/titreItem"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
</TextView>

... in a file res/layout/titreitem.xml as I see anywhere. But I don't understand how get it to put inside my UI. So, inside activity.onCreate, I want to do something like :

RelativeLayout my开发者_开发问答BigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);


Use a LayoutInflater....The Entire Layout can be inflated, without dynamically creating it....

LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);


Approach seems to be little incorrect. You should put RelativeLayout to the xml as your TextView made, and inflate the whole xml. Afterwards, you will be free to add views to your layout. So, do this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+androi:id/relLayout>
  <TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/titreItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  </TextView>
</RelativeLayout>

In your activity:

setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜