2016-04-26 9 views
0

ListViewを持つLinearLayoutを作成する必要があります。listViewの項目が一致しないparent n線形のレイアウト

final ArrayAdapter<TimeInterval> adapter = new ArrayAdapter<>(getContext(), 
                   R.layout.chat_timer_item, 
                   R.id.timer_label, TimeInterval.INTERVALS); 

    listView.setAdapter(adapter); 
:私はこのようなリストビューにアイテムを追加しようとしました作成した後

<ListView 
xmlns:a="http://schemas.android.com/apk/res/android" 
a:layout_width="match_parent" 
a:layout_height="wrap_content" 
a:background="@color/white" 
a:cacheColorHint="@color/white" 
a:paddingBottom="12dp" 
a:paddingTop="12dp"> 

this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    final ListView listView = (ListView) LayoutInflater.from(getContext()).inflate(R.layout.chat_timer_rollout, null); 
    addView(listView); 

マイチャットタイマーの展開は、次のとおりです。私はこのコードでそれをやりました

チャットタイマーアイテムコード:

<TextView 
a:id="@+id/timer_label" 
xmlns:a="http://schemas.android.com/apk/res/android" 
a:layout_width="match_parent" 
a:layout_height="wrap_content" 
a:paddingBottom="11dp" 
a:paddingLeft="@dimen/timer_label_margin_right" 
a:paddingTop="11dp" 
a:textColor="@color/primary_text" 
a:textSize="@dimen/timer_text_size"/> 

これを作成した後、画面上に直線レイアウトが表示されますが、アイテムをクリックすると幅がwrap_contentになり、親に一致しません。どのようにそれを修正するには?私はparam match_parentで膨らませたもう一つの線形レイアウトを設定しようとしましたが、それは助けになりませんでした。ありがとうございました!

+1

'inflate()'呼び出しの2番目のパラメータを 'this'に変更し、' addView() '呼び出しを削除します。 –

+0

@ MikeM.Thank大変ありがとうございます –

答えて

1

LayoutInflater#inflate()メソッドの2番目のパラメータは、拡張されたレイアウトの(オプションの)親Viewです。親にnullを渡すと、InflaterはどのタイプのLayoutParamsが適切かわからないため、デフォルトのViewGroup.LayoutParamsを使用します。これは両方のディメンションでViewGroup.WRAP_CONTENTです。

あなたの投稿コードがあなたのLinearLayoutサブクラスであるので、あなたはinflate()の呼び出しで2番目の引数として渡すことができthis。また、その2つのパラメータの方法で親Viewを提供すると、膨らんだレイアウトが自動的に追加されるので、addView()コールは必要ありません。

final ListView listView = (ListView) LayoutInflater.from(getContext()) 
    .inflate(R.layout.chat_timer_rollout, this); 
関連する問題