0

挨拶します。私はプロジェクトに取り組んでおり、複合コンポーネントをロールアップすると思いました。それらをxmlでレイアウトし、LinearLayoutを拡張する新しいクラスを作成しました。基本的に、アクティビティにはそのうちの5つか6つが必要です。実行前に必要な数がわからないため、これらをアクティビティ内にプログラムで作成したいと思います。また、以下に含めていないxmlファイルを作成して設定しました。私はこれを行うために私が見つけたそれぞれの方法に問題を抱えています。

これが、LinearLayoutを拡張する私が作成したクラス内のメソッドセグメントです。

public ExtendedLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ((Activity)getContext()).getLayoutInflater().inflate(R.layout.extended_linear_layout, this);
    ...
}

そして、アクティビティの中で、これらは私が探しているものに最も近いように見える2つの解決策です。

このメソッドを使用して属性を取得する場所がわからない:

ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs);
layout.addView(customViewClass)

インフレータがこのメソッドでExtendedLinearLayoutを使用するように指定する方法がわかりません。

LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.extended_linear_layout, parent_layout);

私はおそらくListViewの使用に落ち着くでしょうが、5〜6個のアイテムには少しやり過ぎのようです。また、以前にFlexを使用したことがあるので、これは少なくとも問題の合理的な解決策のようです

ありがとう、ランドール

- - - - - - - - アップデート - - - - - - - - - -

以下に説明する方法を試しました。すべてを設定すると、InflationException:Binary XML file line#8:Errorinflatingclassが発生します。xmlは問題ないように見えます私が混乱するかもしれない一つのことは、インフレータをどこと呼ぶかです。現在、カスタムクラスのonFinishInflate()とActivityで次のようにインフレータを呼び出しています。

public class ExtendedLinearLayout extends LinearLayout {
    ...
public ExtendedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.extended_linear_layout, null);
        ...
    }
}

そして、活動では:

public class TestActivity extends Activity {
    LinearLayout list;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (LinearLayout) findViewById(R.id.simple_list);
        runTestCode();
    }
    private void runTestCode() {
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ExtendedLinearLayout layoutOne = 
                (ExtendedLinearLayout) inflater.inflate(R.layout.extended_linear_layout, list);
    }
}

ありがとう、そしてあなたが見てみる時間があることを願っています。私はオンラインで調べて物事を理解するのが好きですが、これは昨日の朝から何時間も料理をしてくれました。これをListAdapterにリファクタリングするだけで約2インチ離れています。

- - - - - - - - アップデート - - - - - - - - - -

これがxmlファイルです

<?xml version="1.0" encoding="utf-8"?>
<com.anotherandroidblog.ExtendedLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/text_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        ></TextView>
</com.anotherandroidblog.ExtendedLinearLayout>
4

1 に答える 1

0

XMLファイルで完全なクラス名を使用する必要があります。ルート要素を作成する代わりに、(パッケージ名を含む)LinearLayoutの完全なクラス名にします。ExtendedLinearLayoutその後、LayoutInflaterは何をすべきかを知っています。

編集:また、attrsオプションは実際には機能しません(または少なくとも私はそうは思いません)。実行時に実際のattrsオブジェクトインスタンスを生成する方法を見つけられませんでした。パブリックコンストラクターはなく、フレームワークが内部で実行するもう1つのことであり、アクセスできないことを期待しています。

于 2011-05-27T14:10:15.987 に答える