2017-07-31 8 views
1

主な目的は、あらかじめ作成されたレイアウトを使用して、編集可能な個別のモジュールを作成し、それをプログラムでルートレイアウトに追加することです。明確にするには、several modules stuck together would look like this.名前、日付、および文字で構成されるクリック可能なブロックをそれぞれ動的に作成したいと思います。次のように各ブロックの.axmlコードは次のとおりです。アンドロイドにレイアウトのコピーをプログラムで作成する方法はありますか?

 <RelativeLayout 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/borderLayout" 
      android:background="@drawable/line" 
      android:paddingBottom="1dp" 
      android:paddingTop="1dp"> 
      <RelativeLayout 
       android:id="@+id/relativeLayout1" 
       android:padding="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:clickable="true" 
       android:background="#ff2f2f2f"> 
       <TextView 
        android:text="C" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/textView1" 
        android:layout_alignParentLeft="true" 
        android:textSize="30dp" /> 
       <TextView 
        android:text="Washington" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/textView1" 
        android:id="@+id/textView2" 
        android:layout_alignParentRight="true" 
        android:gravity="right" /> 
       <TextView 
        android:text="6-8-17" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/textView2" 
        android:id="@+id/textView3" 
        android:gravity="right" 
        android:layout_alignParentRight="true" /> 
      </RelativeLayout> 
     </RelativeLayout> 

私が午前主な問題は、私は.axmlファイルでそれらをフォーマットされたのと同じ方法でプログラムのビューの書式です。

答えて

1

を追加することができます。

その「親」LinearLayoutへの参照を取得します。

いくつかのループで次に
var linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayout1); 

、あなたの繰り返しレイアウトを膨らませるLayoutInflater.Inflateを使用して、ビューを使用するには、返され、そのViewあなたがする必要がある要素のそれぞれにFindViewById更新して増加したインデックスで親ビューに、そのビューを追加します。

index++; 
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, linearLayoutParent, false); 
var letter = view.FindViewById<TextView>(Resource.Id.textView1); 
letter.Text = index.ToString(); 
// FindViewById for textView2, textView3 and assign the text on each.... 
linearLayoutParent.AddView(view, index); 

enter image description here

注:これらの反復要素がたくさんあり、それらをスクロールする必要がある場合(画面外)、代わりにRecyclerViewを使用すると、メモリ管理やスクロールのパフォーマンスなどの面で多くの頭痛を軽減します。 ... ;-)

+0

ありがとう、これは本当に役に立ちます! –

2

インフレータを使用してリソースからビューを作成します。そして、あなたはあなたがに複数のビューを添付したいあなたのメインAXMLでverticalの向きとLinearLayoutを持っていると仮定しましょう、プログラム

context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null); 
関連する問題