简体   繁体   English

for循环在上一次迭代之前停止

[英]for loop stops before last iteration

I've been programming an Android app which is supposed to add TextView s progrmatically. 我一直在编程一个Android应用程序,该应用程序应该以编程方式添加TextView It is supposed to add 28 of them using a for loop. 应该使用for循环添加其中的28个。 Below are the the important parts of the loop: 以下是循环的重要部分:

    for(int i = 0; i < 28; i++){
        Log.i("Creating round", "" + (i + 1));
        final int j = i;
        roundLayout[i] = new LinearLayout(getActivity());
        roundLayout[i].setOrientation(LinearLayout.HORIZONTAL);
        roundLayout[i].setBackgroundColor(Color.WHITE);
        roundLayout[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectRound(j);
            }
        });
        layout.addView(roundLayout[i], lp);
        TextView title = new TextView(getActivity());
        title.setText("Round " + MainActivity.intFormat(i + 1, 2));
        title.setGravity(Gravity.CENTER);
        title.setTextSize(20);
        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        roundLayout[i].addView(title, titleParams);

So as you can see, this loop, in each of its iterations, adds a roundLayout in the main layout , and in each of them adds a Textview title displaying the round number. 如您所见,此循环在其每次迭代中roundLayout在主layout添加一个roundLayout ,并在每个Textview title添加一个显示轮数的Textview title However, while it is supposed to create 28 of them it only creates 27, but the logging goes till 28. 但是,虽然应该创建28个,但是只能创建27个,但是日志记录会持续到28个。

这是应用程序的屏幕截图(滚动到底部)

And here's what appears in the logcat: 这是logcat中显示的内容:

    I/Creating round: 1
    I/Creating round: 2
    I/Creating round: 3
    I/Creating round: 4
    I/Creating round: 5
    I/Creating round: 6
    I/Creating round: 7
    I/Creating round: 8
    I/Creating round: 9
    I/Creating round: 10
    I/Creating round: 11
    I/Creating round: 12
    I/Creating round: 13
    I/Creating round: 14
    I/Creating round: 15
    I/Creating round: 16
    I/Creating round: 17
    I/Creating round: 18
    I/Creating round: 19
    I/Creating round: 20
    I/Creating round: 21
    I/Creating round: 22
    I/Creating round: 23
    I/Creating round: 24
    I/Creating round: 25
    I/Creating round: 26
    I/Creating round: 27
    I/Creating round: 28

EDIT: I've noticed that the container (which's width and height are set to match_parent) in which the layout is inflated may go past the screen (if that's even a thing). 编辑:我注意到,其中布局被夸大的容器(其宽度和高度设置为match_parent)可能会超出屏幕(即使是这样)。 Have a look at the screenshots below: 看看下面的截图:

在此处输入图片说明

在此处输入图片说明

The first screenshot shows the uppermost element of the component tree. 第一个屏幕截图显示了组件树的最上层元素。 We can clearly see the 4 blue lines delimiting it. 我们可以清楚地看到界定它的4条蓝线。 However, the second screenshot shows the container in which the layout is inflated and we can't see the right nor the bottom line delimiting it. 但是,第二张屏幕截图显示了其中的布局已膨胀的容器,我们看不到右边或底线对其进行界定。 Here's the code for the layout: 这是布局的代码:

    <?xml version="1.0" encoding="utf-8"?>
        <androidx.coordinatorlayout.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_weight="1"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name">

            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

     </androidx.coordinatorlayout.widget.CoordinatorLayout>

Thanks in advance! 提前致谢!

The problem seems related to the view not the loop. 问题似乎与视图有关,而不与循环有关。 Here I'm reproducing the views with your code and slightly modified it. 在这里,我将使用您的代码重现视图,并对其进行一些修改。

activity_main.xml layout: activity_main.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  <LinearLayout
      android:id="@+id/parent_rly"
      android:layout_width="match_parent"
      android:orientation="vertical"
      android:layout_height="wrap_content"/>

</ScrollView>

MainActivity.java code: MainActivity.java代码:

public class MainActivity extends AppCompatActivity {
  private LinearLayout[] roundLayout;
  private LinearLayout layout;
  private LinearLayout.LayoutParams lp;
  private LinearLayout.LayoutParams titleParams;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = findViewById(R.id.parent_rly);

    roundLayout = new LinearLayout[28];
    lp= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    titleParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    addingTextViews();
  }

  private void addingTextViews() {
    for (int i = 0; i < 28; i++) {
      Log.i("Creating round", "" + (i + 1));
      roundLayout[i] = new LinearLayout(this);
      roundLayout[i].setOrientation(LinearLayout.HORIZONTAL);
      roundLayout[i].setBackgroundColor(Color.WHITE);

      layout.addView(roundLayout[i], lp);
      TextView title = new TextView(this);
      title.setText("Round " + (i + 1));
      title.setGravity(Gravity.CENTER);
      title.setTextSize(20);
      title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
      roundLayout[i].addView(title, titleParams);
    }
  }
}

and it show the views correctly: 并正确显示视图:

在此处输入图片说明

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

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