简体   繁体   English

将LinearLayout添加到现有XML

[英]Adding a LinearLayout to an existing XML

I have searched in a number of posts here for what I want to accomplish but I find no answer that I can adapt to my purpose. 我在此处搜索了许多帖子以查找想要完成的工作,但找不到适合我的目的的答案。 (As a newbie to C# programming, I am assuming that the C# version I am using is somewhat very new and imposes many strictnesses to the syntax. But my question is not about that...) (作为C#编程的新手,我假设我使用的C#版本有点新,并且对语法施加了许多严格要求。但是我的问题不是这个...)

What I am trying to do is programmatically add to an XML layout. 我正在尝试以编程方式添加到XML布局。

This is my XML: 这是我的XML:

                <LinearLayout
                    android:id="@+id/daterow"
                    android:orientation="horizontal"
                    android:layout_width="480dp"
                    android:layout_height="40dp"
                    android:background="@drawable/customborder"
                    android:padding="0dp">
                <!--                        <LinearLayout
                        android:id="@+id/ll_001"
                        android:orientation="horizontal"
                        android:layout_width="20dp"
                        android:layout_height="40dp"
                        android:background="@drawable/customborder">
                        <TextView
                            android:id="@+id/Slot_001"
                            android:text="00:00"
                            android:textSize="8dp"
                            android:layout_width="20dp"
                            android:layout_height="40dp"
                            android:rotation="270"
                            android:gravity="left|center"
                            android:singleLine="true"
                            android:maxLines="1" />
                    </LinearLayout> -->

My code snippet is as follows: 我的代码段如下:

            LinearLayout parentLayout = (LinearLayout)FindViewById(Resource.Id.daterow);
            LinearLayout Linear1 = new LinearLayout(this);
            Linear1.LayoutParameters = new LayoutParams(LayoutParams.MatchParent,LayoutParams.WrapContent);
            parentLayout.AddView(Linear1);


            TextView tv = new TextView(this);

            tv.Id = i;  // i is variable in a for loop inside of which this code is.
            int qq;   //THIS LINE FOR TESTING ONLY.
            qq = 333;  //THIS LINE FOR TESTING ONLY.
            this.tv.SetText(DateTime.Now.AddMinutes(this.iSlot).ToString("HH:mm"));  //THIS IS WHAT I WANT TO ACCOMPLISH EVENTUALLY
            tv.SetText(qq);  //THIS LINE FOR TESTING ONLY. **GIVES ERROR**
            ll2.AddView(tv);  //THIS LINE FOR TESTING ONLY.

Ultimately, I want to programmatically achieve an XML as shown in the section "This is my XML".. 最终,我想以编程方式实现XML,如“这是我的XML”部分所示。

Visual Studio does not highlight any error in the syntax in the above. Visual Studio不会在上面的语法中突出显示任何错误。 But during runtime - I get the following error: 但是在运行时-我收到以下错误:

Unhandled Exception: 未处理的异常:

Android.Content.Res.Resources+NotFoundException: String resource ID #0x14d Android.Content.Res.Resources + NotFoundException:字符串资源ID#0x14d

Can someone tell me what error I am committing? 有人可以告诉我我犯了什么错误吗?

In your response could you also please include which "using" I should have to use the functions that you suggest in your solution - as I am a newbie at C#. 在您的答复中,您还可以包括我应该使用您在解决方案中建议的功能时使用的“使用”吗?因为我是C#的新手。

Adding a LinearLayout to an existing XML 将LinearLayout添加到现有XML

 LinearLayout parentLayout = (LinearLayout)FindViewById(Resource.Id.daterow);

 //using Android.Widget;
 Android.Widget.LinearLayout Linear1 = new Android.Widget.LinearLayout(this);

 //using Android.Views.ViewGroup;
 Linear1.LayoutParameters = new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.WrapContent);
 parentLayout.AddView(Linear1);

 Android.Widget.TextView tv = new Android.Widget.TextView(this);
 tv.Text = DateTime.Now.AddMinutes(1.1).ToString("HH:mm");//THIS IS WHAT I WANT TO ACCOMPLISH EVENTUALLY
 Linear1.AddView(tv);  //THIS LINE FOR TESTING ONLY.

Android.Content.Res.Resources+NotFoundException: String resource ID #0x21 Android.Content.Res.Resources + NotFoundException:字符串资源ID#0x21

This happened when you use tv.SetText(qq) method, if you just want to set a text for TextView , you could use tv.Text = qq.ToString() . 使用tv.SetText(qq)方法时会发生这种情况,如果只想为TextView设置文本,则可以使用tv.Text = qq.ToString()

this.tv.SetText(DateTime.Now.AddMinutes(this.iSlot).ToString("HH:mm")); this.tv.SetText(DateTime.Now.AddMinutes(this.iSlot)的ToString( “HH:MM”)); //THIS IS WHAT I WANT TO ACCOMPLISH EVENTUALLY //这是我想要完成的

Modify your code like this you could accomplish the effect : 像这样修改代码,就可以实现效果:

tv.Text = DateTime.Now.AddMinutes(1.1).ToString("HH:mm");

Effect : 效果

在此处输入图片说明

Thanks to York Shen, I was able to come up with some basic principles which I used - summarized below: 多亏了York Shen,我得以提出一些我使用的基本原理-总结如下:

  1. Start with setting focus to the existing-in-axml LinearLayout under which you want to add another Linear Layout. 首先将焦点设置为要在其下添加另一个线性布局的现有XML线性布局。 I used LinearLayout dtlr = (LinearLayout)FindViewById(Resource.Id.datetimelogorow); 我使用LinearLayout dtlr = (LinearLayout)FindViewById(Resource.Id.datetimelogorow); I have a LinearLayout in my axml file called datetimelogorow. 我的axml文件中有一个名为datetimelogorow的LinearLayout。

  2. Next define your new Linear Layout thus: Android.Widget.LinearLayout dtr = new Android.Widget.LinearLayout(this); 接下来, 定义新的线性布局: Android.Widget.LinearLayout dtr = new Android.Widget.LinearLayout(this);

  3. Change all parameters as desired such as orientation, background etc using (for example) dtr.Orientation = Orientation.Horizontal; 使用dtr.Orientation = Orientation.Horizontal;根据需要更改所有参数,例如方向,背景等dtr.Orientation = Orientation.Horizontal;

  4. Now create your LinearLayout thus dtlr.AddView(dtr); 现在创建dtlr.AddView(dtr);

  5. Text view under LinearLayout can also be added similarly as in Step 2 replacing both the "LinearLayout" by "TextView" in that line. 也可以像在第2步中一样添加LinearLayout下的文本视图,在该行中将“ LinearLayout”替换为“ TextView”。

Hope this helps. 希望这可以帮助。

Thanks to York Shen once again. 再次感谢York Shen。

Uttam UTTAM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM