有没有办法在Android中以编程方式创建布局的副本? [英] Is there a way to programmatically create copies of a layout in android?

查看:73
本文介绍了有没有办法在Android中以编程方式创建布局的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要目标是使用预制的布局创建可以编辑的单独模块,然后以编程方式将其添加到根布局.为了明确起见,粘在一起的几个模块看起来像这样.我想动态创建每个可点击的块,由名称,日期和字母组成.每个块的.axml代码如下:

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>

我遇到的主要问题是以与在.axml文件中格式化视图相同的方式,以编程方式格式化视图.

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

推荐答案

假设您在主axml中有一个LinearLayout且方向为vertical,并且希望将其附加到多个视图.

Lets assume you have a LinearLayout with an orientation of vertical in your main axml that you wish to attach multiple views to.

获取对该父级" LinearLayout的引用:

Get a reference to that "parent" LinearLayout:

var linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayout1);

然后在某个循环中,使用LayoutInflater.Inflate扩大重复的布局,使用返回的视图,并在该View上使用FindViewById,您需要更新的每个元素,然后将该视图添加到父视图中递增指数:

Then in some loop, use LayoutInflater.Inflate to inflate your repeating layout, use the view returned and FindViewById on that View each of the elements you need to update and then add that view to the parent view with an increasing index:

index++;
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, linearLayoutParent, false);
var letter = view.FindViewById<TextView>(Resource.Id.textView1);
letter.Text = index.ToString();
// FindViewById for textView2, textView3 and assign the text on each....
linearLayoutParent.AddView(view, index);

注意:如果您有很多重复元素,并且必须滚动它们(屏幕外),请改用RecyclerView查看,这将使您在内存管理,滚动方面省去很多麻烦性能等...;-)

Note: If you have a lot of these repeating elements and you will have to scroll them (off screen), look at using a RecyclerView instead, it will save you a lot of headaches into terms of memory management, scrolling performance, etc... ;-)

这篇关于有没有办法在Android中以编程方式创建布局的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆