在Android中以编程方式操作XML布局

Hik*_*han 3 xml android

我们可以在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());

}
Run Code Online (Sandbox Code Playgroud)

}

我的Xml布局如下.

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

此致,Hikmat Khan

ing*_*abh 6

是的,你可以有夸大的UI xml使用LayoutInflater,然后你可以从UI元素xml.有权访问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);
Run Code Online (Sandbox Code Playgroud)