【发布时间】:2017-07-06 07:29:34
【问题描述】:
我在这里的许多帖子中搜索了我想要完成的任务,但我没有找到可以适应我的目的的答案。 (作为 C# 编程的新手,我假设我使用的 C# 版本有点新,并且对语法施加了许多严格性。但我的问题不是那个......)
我正在尝试以编程方式添加到 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> -->
我的代码sn-p如下:
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.
最终,我想以编程方式实现 XML,如“这是我的 XML”部分所示。..
Visual Studio 不会突出显示上述语法中的任何错误。但在运行时 - 我收到以下错误:
未处理的异常:
Android.Content.Res.Resources+NotFoundException:字符串资源 ID #0x14d
谁能告诉我我犯了什么错误?
在您的回复中,您能否还包括哪些“使用”我应该使用您在解决方案中建议的功能 - 因为我是 C# 的新手。
【问题讨论】:
标签: c# android xamarin.android visual-studio-2017