我有一个UI,我动态构建它.我想将一些组件放在xml资源文件中.所以我这样做:
<?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>
Run Code Online (Sandbox Code Playgroud)
...在res/layout/titreitem.xml我看到的任何地方的文件中.但是我不明白如何将它放到我的UI中.所以,在里面activity.onCreate,我想做的事情如下:
RelativeLayout myBigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);
Run Code Online (Sandbox Code Playgroud)
st0*_*0le 28
使用LayoutInflater ....整个布局可以膨胀,而无需动态创建....
LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);
Run Code Online (Sandbox Code Playgroud)
方法似乎有点不正确.您应该将RelativeLayout作为TextView制作成xml,并为整个xml充气.之后,您可以自由地为布局添加视图.所以,这样做:
<?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>
Run Code Online (Sandbox Code Playgroud)
在您的活动中:
setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18295 次 |
| 最近记录: |