Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

c# - Is there a way to programmatically create copies of a layout in android?

The main goal is to use a pre-made layout to create separate modules that can be edited, and then programmatically add them to the root layout. To clarify, several modules stuck together would look like this. I would like to dynamically create each clickable block that consists of a name, date, and letter. The .axml code for each block is as follows:

        <RelativeLayout
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/borderLayout"
            android:background="@drawable/line"
            android:paddingBottom="1dp"
            android:paddingTop="1dp">
            <RelativeLayout
                android:id="@+id/relativeLayout1"
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="#ff2f2f2f">
                <TextView
                    android:text="C"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView1"
                    android:layout_alignParentLeft="true"
                    android:textSize="30dp" />
                <TextView
                    android:text="Washington"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/textView1"
                    android:id="@+id/textView2"
                    android:layout_alignParentRight="true"
                    android:gravity="right" />
                <TextView
                    android:text="6-8-17"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textView2"
                    android:id="@+id/textView3"
                    android:gravity="right"
                    android:layout_alignParentRight="true" />
            </RelativeLayout>
        </RelativeLayout>

The main problem I am having is formatting the views programmatically in the same way that I formatted them in the .axml file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use inflater to create a view from resource. Then you can add it programmatically

context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...