【问题标题】:How to use String array with multiple LinearLayouts in Android Studio如何在 Android Studio 中使用带有多个 LinearLayout 的字符串数组
【发布时间】:2020-04-24 05:50:04
【问题描述】:

我想使用 String 数组作为 LinearLayouts 的增量,如下所示,以根据当前月份的 12 个月 JAN 到 DEC 的背景颜色变化。但它根本没有按照下面的代码工作。我缺少什么,请提供正确的 sn-p 示例。

String[] string = {linear_layout1, linear_layout2, linear_layout3, linear_layout4, linear_layout5, linear_layout6, linear_layout7, linear_layout8, linear_layout9, linear_layout10, linear_layout11, linear_layout12};
   if(monthOfYear <= 12) {
      for(int i = 1; i <= 12; i++) 
         {
            string.setBackgroundColor(getResources().getColor(R.color.GRAY));
         }
   }

提前谢谢你..

【问题讨论】:

  • 请添加错误日志和更多上下文,说明它为什么不工作。这些 linear_layout* 数据​​类型是什么?
  • 在 linear_layout* 和 string.setBackgroundColor 处显示错误(红色标记)。这些linear_layout* 是.xmlids。我想使用这些linearlayout ids 和string array 列表来更改linearlayouts 背景颜色。希望你明白我的意思。

标签: android string android-studio android-linearlayout


【解决方案1】:

所以当我收到您的问题时,您有很多视图要更改它们的背景颜色,对吗? 如果是这种情况,那么您的代码有问题,解决方法是:

LinearLayout[] lns= {linear_layout1, linear_layout2, linear_layout3, linear_layout4, linear_layout5, linear_layout6, linear_layout7, linear_layout8, linear_layout9, linear_layout10, linear_layout11, linear_layout12};
   if(monthOfYear <= 12) {
      for(int i = 0; i <= 11; i++) 
         {
            lns[i].setBackgroundColor(getResources().getColor(R.color.GRAY));
         }
   }

首先,LinearLayouts 是 Views,你需要有一个 LinearLayouts 数组或 Views 而不是 String[](为什么是 String?) 第二个问题是您正在从数组中读取数据,因此您需要使用您在 for 循环中声明的“i”变量获取对象,并让每个 LinearLayout 对其进行更改和处理。 为了更容易,我将 for 循环的开始从 1 更改为 0,将结束从 12 更改为 11。 (您可以使用 for-each 而不是 for 它会更容易......) 还有什么问题吗?随便问问。

编辑

好吧,我想我看到了您的 cmets,我认为这些 linear_layout1,... 只是您的 .xml 文件中的 ID。正确的?在这种情况下,你的代码应该是这样的:

LinearLayout[] lns= {(LinearLayout)findViewById(R.id.linear_layout1),(LinearLayout)findViewById(R.id.linear_layout2),(LinearLayout)findViewById(R.id.linear_layout3),(LinearLayout)findViewById(R.id.linear_layout4),(LinearLayout)findViewById(R.id.linear_layout5),(LinearLayout)findViewById(R.id.linear_layout6),(LinearLayout)findViewById(R.id.linear_layout7),(LinearLayout)findViewById(R.id.linear_layout8),(LinearLayout)findViewById(R.id.linear_layout9),(LinearLayout)findViewById(R.id.linear_layout10),(LinearLayout)findViewById(R.id.linear_layout11),(LinearLayout)findViewById(R.id.linear_layout12)};
      for(int i = 0; i <= 11; i++) 
         {
            lns[i].setBackgroundColor(getResources().getColor(R.color.GRAY));
         }

我认为 if 语句也没用,因为 for 循环总是 12 次,那么 if 是什么意思?所以我也删除了。

【讨论】:

  • 感谢您及时回复详细信息。我已按照上面的代码进行了更改,但应用程序在活动打开时崩溃,没有任何 Logcat 错误。
  • 是的,当然,因为Linears不清楚,它们只是名称和示例。请添加您的完整活动代码,以便更好地指导您。
  • 嗨@Amir,代码工作如下,但有一个问题,12 个线性布局背景突出显示,而不是当前的 ll 月。修改后的代码:LinearLayout[] lns= {l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12}; for(i = 0; i &lt;= 11; i++) {if(dayOfMonth == i) {lns[inc].setBackgroundColor(getResources().getColor(R.color.GRAY));}}。我尝试了很多方法来仅突出当前月份,但没有任何效果。你能告诉我我做错了什么吗?
  • 如果这是您更新的代码,那么lns[inc] 就是问题所在。 inc 是什么?你必须使用lns[I]
  • 好的,那么当我从您在上述帖子中的评论中读到时,我认为这些 l1,l2,l3,... 只是线性布局的 id,所以如果是这种情况,那么您需要 findViewById(R.id.ln1);findViewById(R.id.ln2);... 而不是只是单独 ln1,ln2,... 它们可以用作视图。
猜你喜欢
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多