在Android中以编程方式操作XML布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Android中以编程方式操作XML布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含12564字,纯文字阅读大概需要18分钟。
内容图文
![在Android中以编程方式操作XML布局](/upload/InfoBanner/zyjiaocheng/748/8bfe8929ee454f14aeb5bdb2de31accb.jpg)
我们可以在Java代码中操作Xml布局吗?我的意思是让我说我??在Xml中有一个布局,想要创建它的对象并改变它的属性(比如在将它传递给setcontentveiw()方法之前添加一些子)
public class IconExtensionLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mcontainer = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.mContainerIconExtension);
ScrollView mScrollView = (ScrollView)IconExtensionLayoutActivity.this.findViewById(R.id.mRootIconExtension);
RelativeLayout mRow1 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_One_ID);
RelativeLayout mRow2 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_Two_ID);
RelativeLayout mRow3 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_Three_ID);
setContentView(mScrollView.getId());
}
}
我的Xml布局如下.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mContainerIconExtension"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="none" >
<RelativeLayout
android:id="@+id/Root_Layout_Iconset_One_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/IconSet_One_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:id="@+id/IconSet_One_ScrollView_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/IconSet_One_ID"
android:layout_margin="5dip" >
<HorizontalScrollView
android:id="@+id/HorizontalScrollView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ImageView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<ImageView
android:id="@+id/PURCHASE_BUTTON_ICONSET_1_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/IconSet_One_ScrollView_ID"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/Root_Layout_Iconset_Two_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:id="@+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ImageView17"
android:layout_margin="5dip" >
<HorizontalScrollView
android:id="@+id/Horizontal_Scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ImageView51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView47"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView46"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<ImageView
android:id="@+id/ImageView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/RelativeLayout02"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/Root_Layout_Iconset_Three_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:id="@+id/RelativeLayout04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ImageView27"
android:layout_margin="5dip" >
<HorizontalScrollView
android:id="@+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ImageView42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/ImageView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<ImageView
android:id="@+id/ImageView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/RelativeLayout04"
android:src="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
</LinearLayout>
问候,
希克马特汗
解决方法:
是的,您可以使用LayoutInflater来扩展xml的UI,然后您可以从xml获取UI元素.有权访问UI对象,您可以在其上添加额外的UI元素层,最后将它们设置为setContentView()
LayoutInflater mInflater = LayoutInflater.from(this);
View contentView = mInflater.inflate(R.layout.main, null);
LinearLayout root = (LinearLayout) contentView.findViewById(R.id.root);// mContainerIconExtension in your case
root.addView(new TextView(this));
setContentView(root);
内容总结
以上是互联网集市为您收集整理的在Android中以编程方式操作XML布局全部内容,希望文章能够帮你解决在Android中以编程方式操作XML布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。