0

カテゴリごとに動的に別のレイアウトで膨らませるレイアウトがあります。

LinearLayout l = (LinearLayout) findViewById(R.id.container);

        for (CategoryEB e :categoryList){
            LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout linLayout = (LinearLayout)layoutInflater.inflate(R.layout.inflated_layout, l);
            TextView view = (TextView)linLayout.findViewById(R.id.lable);
            view.setText(e.getName());
        }

これは、拡張されるxml(inflated_layout.xml)です。

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

    <TextView android:id="@+id/lable"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="xxx"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="6dip"/>

</LinearLayout>

ただし、この方法では、最初のアイテムのみに正しい値が入力されます。categoryListの他のものはxxxとして表示されます(xmlで説明されています)。

リストに正しい名前をすべて表示するにはどうすればよいですか?

4

3 に答える 3

2

最近、同じ問題が2回発生しました。初めてLinearLayoutを使用したので、最初の答えはうまくいきました-すべてのビューIDを0に設定することは問題ではありませんでした。

ただし、2回目にRelativeLayoutを使用した場合、idを0に設定すると、「leftTo:some_id」などとして配置されるため、ビュー全体が壊れてしまいます。

少し試してみたところ、すべてのコンポーネントを設定した後でビューをコンテナに追加することで問題が解決することがわかりました(親に膨らんだレイアウトを追加できないようにするパラメータでビューを膨らませる必要があります)。アイデアは、同じIDを持つコンポーネントを含む他のビューの外側に膨らんだビューを配置することです。したがって、findViewByIdは、以前に膨らんだものではなく、関心のあるものだけを見つけることができます。

ViewGroup inflatedLayout = (ViewGroup) layoutInflater.inflate(R.layout.some_id, mDirectChildInScrollView, false);
ImageView imageView = (ImageView) inflatedLayout.findViewById(R.id.image_view);
... do your stuff here ...
mDirectChildInScrollView.addView(inflatedLayout);

ViewHolderパターンを使用して、後でfindViewByIdを呼び出さずに目的の要素にアクセスできます。

于 2014-10-31T12:59:47.323 に答える
1

問題は、linLayoutがメインのレイアウトコンテナlを指すことです。そのため、IDがR.id.labelのビューの最初のインスタンスのみが見つかります。

簡単な解決策の1つは、ラベルを初期化したら、ラベルのIDを変更することです。このようなもの:

LinearLayout l = (LinearLayout) findViewById(R.id.container);

    for (CategoryEB e :categoryList){
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linLayout = (LinearLayout)layoutInflater.inflate(R.layout.inflated_layout, l);
        TextView view = (TextView)linLayout.findViewById(R.id.lable);
        view.setText(e.getName());
        view.setId(0);  //this way you wont find the same view twice.
    }
于 2011-07-27T09:43:01.450 に答える
0

同じビューを繰り返し複製して編集しようとするのではなく、ループ内に新しいTextViewを動的に作成する必要があるように思われます。論理的には、これは問題のようです。categoryListのサイズのtextviewの配列を作成し、それを使用して、ループで作成した新しいTextViewへの参照を格納します。

TextView textViewArray[] = new TextView[categoryList.length];

LinearLayout l = (LinearLayout) findViewById(R.id.container);
LinearLayout linLayout = (LinearLayout) findViewById(R.id.CREATE_A_LIN_LAYOUT_IN_YOUR_CONTAINER_XML);

int i = 0; //Used for array addressing

    for (CategoryEB e :categoryList){
        TextView view = new TextView(); //declare new TextView
        view.setText(e.getName()); //set Text
        textViewArray[i] = view; //Save into array for future reference
        linLayout.addView(view); //Add TextView to linearLayout
        i++;
    }
于 2011-07-27T06:27:14.057 に答える