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

时间:2019-03-17 12:30:07

标签: java android

我一直在编写一个Android应用,该应用应该以编程方式添加TextView。应该使用for循环添加其中的28个。以下是循环的重要部分:

    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);

如您所见,此循环在其每次迭代中都在主roundLayout中添加了一个layout,并在每个循环中都添加了一个显示轮数的Textview title 。但是,虽然应该创建28个,但是只能创建27个,但是日志记录会持续到28个。

Here's a screenshot from the app (scrolled to the bottom)

这是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

编辑: 我注意到,布局被放大的容器(宽度和高度设置为match_parent)可能会超出屏幕(即使是这样)。看看下面的截图:

enter image description here

enter image description here

第一个屏幕截图显示了组件树的最上层元素。我们可以清楚地看到界定它的4条蓝线。但是,第二张屏幕截图显示了其中的布局已膨胀的容器,我们看不到右边或底线对其进行界定。这是布局的代码:

    <?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>

谢谢!

1 个答案:

答案 0 :(得分:-1)

问题似乎与视图有关,而不与循环有关。在这里,我将使用您的代码重现视图,并对其进行一些修改。

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代码:

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);
    }
  }
}

并正确显示视图:

enter image description here