簡體   English   中英

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

[英]for loop stops before last iteration

我一直在編程一個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添加一個roundLayout ,並在每個Textview title添加一個顯示輪數的Textview title 但是,雖然應該創建28個,但是只能創建27個,但是日志記錄會持續到28個。

這是應用程序的屏幕截圖(滾動到底部)

這是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)可能會超出屏幕(即使是這樣)。 看看下面的截圖:

在此處輸入圖片說明

在此處輸入圖片說明

第一個屏幕截圖顯示了組件樹的最上層元素。 我們可以清楚地看到界定它的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>

提前致謝!

問題似乎與視圖有關,而不與循環有關。 在這里,我將使用您的代碼重現視圖,並對其進行一些修改。

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

並正確顯示視圖:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM