0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

//this is where I want the progressbars to be.
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

xmlファイルで指定せずに、ユーザーが指定した数のプログレスバーをプログラムでレイアウトに作成したいと思います。これを理解するのを手伝ってください。

御時間ありがとうございます!

4

1 に答える 1

1

これらのプログレスバー(実際にはすべてのウィジェット)のコンテナーを配置することをお勧めしますex

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <LinearLayout 
        android:id="@+id/linBars"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    </LinearLayout>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

次に、コード内、つまりアクティビティ内(基本的にはオブジェクトへのアクセスが必要Context)で、コンテナーにID「linBars」を指定したことに注意してください。これは、任意の有効なJava識別子にすることができます(R.idに入れられます)。

 //Get the container
 LinearLayout container = (LinearLayout) findViewById(R.id.linBars);


 ProgressBar pbar = new ProgressBar(this);
 //Set any properties for it here (eg setInterpolator etc)
 container.addView(pbar); //ProgressBar and just about any widget is a subclass of View

「コンテナへのウィジェットの追加/削除」コードのほとんどが存在するViewGroupsを読むことをお勧めします。はそのサブクラスであり、他のいくつかを使用することもできますが、多くのインスタンスLinearLayoutに最適です。ProgressBar

コメントに答えるには作成したウィジェットをどのように参照しますか?

独自の長所と短所を持つ2つの方法

  • LinearLayoutウィジェットを保持するリストまたは配列を作成し、ウィジェットをリスト/配列に追加する前または後にウィジェットを追加します。あなたはあなたがあなたの家を掃除してそれらへのすべての参照を削除する必要があることを除いてあなたが望むすべてをそれらを参照することができますOnDestroy、さもなければあなたのプログラムはメモリを使い果たし、親が破壊されたウィジェットで何かを呼び出すとそれは何もしないか爆発します(別名、例外をスローします)。
  • ViewGroup実際には、その下にあるすべてのウィジェットにアクセスできます。残念ながら、取得するウィジェットの順序や種類については保証されません。getChildAt(int index)このためのメソッドはとですgetChildCount()。オブジェクトを返すViewので、すべての子が特定のタイプであることが確実にわかっている場合にのみ、安全にキャストできることに注意してください。私が提供したSDKリンクで説明されているもう1つの注意点は、指定したインデックスに子がない場合はnullを返します。この例では、追加されたすべてのビューが次のProgressBarようになることがわかっています。

すべてのプログレスバーを取得する例:

  //Get the container
  LinearLayout container = (LinearLayout) findViewById(R.id.linBars);

  ProgressBar pbar;
  for (int i = 0; i < container.getChildCount(); i++) {
      pbar = (ProgressBar) container.getChildAt(i);
      //Do whatever to the progress bar here
  }

また、別の問題も指摘しなければならないと感じています。ユーザーがアプリを離れた場合(つまり、ホームボタンを押して電話をかけた場合)、しばらくするとシステムがアクティビティを破棄してメモリを節約することを決定し、ユーザーがアプリに戻ると、情報を保存しない限りプログレスバーが表示されなくなりますBundle中にそれらを再構築し、のアイテムをOnDestroyチェックインしてから、に再追加します。OnCreateBundlelinearLayout

于 2012-07-07T21:02:09.760 に答える