【问题标题】:Adding a LinearLayout to an existing XML将 LinearLayout 添加到现有 XML
【发布时间】: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


    【解决方案1】:

    向现有 XML 添加 LinearLayout

     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:字符串资源 ID #0x21

    这发生在你使用tv.SetText(qq)方法时,如果你只想为TextView设置一个文本,你可以使用tv.Text = qq.ToString()

    this.tv.SetText(DateTime.Now.AddMinutes(this.iSlot).ToString("HH:mm")); //这就是我最终想要实现的目标

    这样修改你的代码就可以达到效果了:

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

    效果

    【讨论】:

    • 不幸的是,我在这一行收到未处理的异常错误: parentLayout.AddView(Linear1);未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。
    • @Uttam Padukone,你能更新你的代码吗?这对我来说很好用。
    • 感谢您的快速回复 - 这是代码: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);
    • 逻辑没有问题。如果可能,请分享您的项目,或发布您的 .axml 文件。
    • &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;HorizontalScrollView android:id="@+id/fulldisplay" &lt;LinearLayout android:id="@+id/datetimelogorow" &lt;LinearLayout android:id="@+id/datetimerow" &lt;LinearLayout android:id="@+id/currentdatebox"&gt; &lt;LinearLayout android:id="@+id/daterow"&gt; &lt;/LinearLayout&gt; &lt;!-- WANT TO ADD Linear Layout here --&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/HorizontalScrollView&gt; &lt;/LinearLayout&gt;
    【解决方案2】:

    感谢York Shen,我能够提出一些我使用的基本原则 - 总结如下:

    1. 首先将焦点设置到要在其下添加另一个线性布局的现有 axml 线性布局。我使用了LinearLayout dtlr = (LinearLayout)FindViewById(Resource.Id.datetimelogorow); 我的 axml 文件中有一个 LinearLayout,名为 datetimelogorow。

    2. 接下来定义你的新线性布局:Android.Widget.LinearLayout dtr = new Android.Widget.LinearLayout(this);

    3. 使用(例如)dtr.Orientation = Orientation.Horizontal;

    4. 根据需要更改所有参数,例如方向、背景等
    5. 现在创建您的 LinearLayout 从而dtlr.AddView(dtr);

    6. LinearLayout 下的文本视图也可以像第 2 步一样添加,在该行中将“LinearLayout”替换为“TextView”。

    希望这会有所帮助。

    再次感谢York Shen。

    乌塔姆

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多