我们可以在Java代码中 *** 作Xml布局吗?我的意思是让我说我在Xml中有一个布局,想要创建它的对象并改变它的属性(比如在将它传递给setcontentveiw()方法之前添加一些子)
public class IconExtensionLayoutActivity extends Activity {@OverrIDepublic 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 caseroot.addVIEw(new TextVIEw(this));setContentVIEw(root);
总结 以上是内存溢出为你收集整理的在Android中以编程方式 *** 作XML布局全部内容,希望文章能够帮你解决在Android中以编程方式 *** 作XML布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)