1

レイアウトを含むViewFlipperがあります。

クラスを接続して各レイアウトを個別に管理する方法はありますか?

ちょっと似setContentView(R.layout.main);ていますが、コードで3つの異なるレイアウトを参照する方法がわかりません。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
          <ListView android:layout_width="wrap_content" android:id="@+id/ListView" android:layout_height="220dp"></ListView>
          <ViewFlipper android:id="@+id/ViewFlipper" android:layout_width="fill_parent" android:layout_height="fill_parent">
                <LinearLayout android:id="@+id/LinearLayoutST" android:layout_width="fill_parent" android:layout_height="fill_parent">
                      <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon"></ImageView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutChart" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 2"></TextView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutDetails" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 3"></TextView>
                </LinearLayout>
          </ViewFlipper>
    </LinearLayout>
4

1 に答える 1

0

これは、次の 2 つの方法で行うことができます。

  1. 対応するクラス インスタンスを、操作するビューまたはレイアウトにアタッチします。通常の呼び出しを使用したいだけで、OnClickListeners などを設定する場合は、これで十分です。

    LinearLayout layoutChart = (LinearLayout)findViewById(R.id.LinearLayoutChart);

  2. デフォルトのクラス動作 (つまりサブクラス) をオーバーライドする必要がある場合は、拡張クラスを作成します。

    public class LinearLayoutChart extends LinearLayout { ...}

すべての親コンストラクター (super の呼び出しに渡すだけでも...)、特に属性を取るものを必ず実装してください。

カスタマイズしたコードを拡張クラスに実装します。

次に、レイアウト xml を編集し、LinearLayout タグと終了タグを、パッケージ名を含む特別なクラスの完全修飾クラス名に置き換えます。

<com.mycompany.mypackage.LinearLayoutChart>
<!-- layout definition as before -->
</com.mycompany.mypackage.LinearLayoutChart>

これにより、レイアウト インフレータがストック クラスの代わりにクラスをインスタンス化するため、保護されたメソッドなどをオーバーライドできます。

于 2010-09-02T07:43:30.407 に答える