2012-05-16 48 views
2

我試圖創建自己的自定義複合控件,並開始在Activity上開發它,然後將相同的代碼放入從AndroidLayer文檔中指示的LinearLayout擴展的類中。這是我的我的活動的代碼,其中IM實例我自己定製的控制:在monodroid上自定義複合控件

[Activity (Label = "GridControl", MainLauncher = true)] 
public class Main : Activity 
{  
    private GridDataView<Customer> gridDataView; 

    private int fieldsCount; 
    private List<Customer> customers; 

    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 


     customers = Customer.GetCustomers(); 

     int customizeFieldsCount = typeof(Customer).GetProperties().Length; 
     string[] columnItems = {"Customer Number", "Customer Name", "City", "Address", "Credit Limit", "Contact Name", "Telephone Number", "Mail"}; 

     gridDataView = new GridDataView<Customer>(this, customers, customizeFieldsCount, columnItems); } } 

這是定製的複合控制代碼:

public class GridDataView<T> : LinearLayout 
{ 
    private ScrollView sc; 
    private HorizontalScrollView hsc; 
    private RelativeLayout rl1, rl2; 
    private TableLayout tl1, tl2; 

    private List<T> dataObject; 
    private int fieldsCount; 
    private string[] columnItems; 

    public GridDataView(Context context, List<T> dataObject, int fieldsCount, string[] columnItems) 
     : base (context) 
    { 
     try 
     { 
      this.Orientation = Android.Widget.Orientation.Vertical; 
      sc = new ScrollView(context); 
      rl1 = new RelativeLayout(context); 
      hsc = new HorizontalScrollView(context); 
      tl1 = new TableLayout(context); 
      tl2 = new TableLayout(context); 

      //Create a new row to be added. 
      TableRow trHeader = new TableRow(context); 
      TableRow trLeftHeader = new TableRow(context); 

       //Create text views to be added to the row. 
       for (int i = 0; i < fieldsCount; i++) 
       { 
        TextView tvHeader = new TextView(context); 
        if(i==0) 
        { 
         CreareHeader(trLeftHeader, tvHeader, columnItems[i]); 
        } 
        else 
         CreareHeader(trHeader, tvHeader, columnItems[i]); 
       } 

       tl1.AddView(trLeftHeader); 
       tl2.AddView(trHeader); 

       var dataProperties = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); 

       for(int i = 0; i<dataObject.Count(); i++) 
       { 
       //Create a new row to be added 
       TableRow trData = new TableRow(context); 
       TableRow trLeftData = new TableRow(context); 

       for (int j = 0; j < fieldsCount; j++) { 

       TextView tv = new TextView(context); 
       if(j==0) 
       { 
        CreateLeftColumn(trLeftData, tv, dataProperties[j].GetValue(dataObject[i], null).ToString()); 
       } 
       else 
        CreateView(trData, tv, dataProperties[j].GetValue(dataObject[i], null).ToString()); 
       } 

       //Add the new row to our tableLayout tl 
       tl1.AddView(trLeftData); 
       tl2.AddView(trData); 
       }  


      RelativeLayout.LayoutParams parameters = new RelativeLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent); 
      parameters.AddRule(LayoutRules.RightOf, tl1.Id); 
      hsc.AddView(tl2, parameters);; 
      rl1.AddView(tl1); 
      rl1.AddView(hsc); 
      sc.AddView(rl1); 
     } 
     catch(Exception ex) 
     { 
      Toast.MakeText(context, ex.ToString(), ToastLength.Long).Show(); 
     } 
    } 

    public void SetDataObject(List<T> mDataObject) 
    { 
     dataObject = mDataObject; 
    } 

    public void SetFieldsCount(int mFieldsCount) 
    { 
     fieldsCount = mFieldsCount; 
    } 

    public void SetColumnItems(string[] mColumnItem) 
    { 
     columnItems = mColumnItem; 
    } 

    private void CreareHeader(TableRow tr, TextView t, string viewdata) 
    { 
     t.SetText(viewdata, TextView.BufferType.Editable); 

     //You have to use Android.Graphics.Color not System.ConsoleColor; 
     t.SetTextColor(Android.Graphics.Color.White); 
     t.SetBackgroundColor(Android.Graphics.Color.Black); 
     t.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold); 
     t.SetPadding(10, 10, 10, 10); 
     t.Gravity = GravityFlags.CenterHorizontal; 

     tr.SetPadding(0, 1, 0, 1); 
     tr.SetBackgroundColor(Android.Graphics.Color.Gray); 

     tr.AddView(t); // add TextView to row. 
    } 

    private void CreateLeftColumn(TableRow tr, TextView t, string viewdata) 
    { 
     t.SetText(viewdata, TextView.BufferType.Editable); 

     //You have to use Android.Graphics.Color not System.ConsoleColor; 
     t.SetTextColor(Android.Graphics.Color.White); 
     t.SetBackgroundColor(Android.Graphics.Color.Black); 
     t.SetPadding(5, 0, 0, 0); 
     t.Gravity = GravityFlags.Left; 

     tr.SetPadding(0, 1, 0, 1); 
     tr.SetBackgroundColor(Android.Graphics.Color.Gray); 

     tr.AddView(t); // add TextView to row. 

    } 

    private void CreateView(TableRow tr, TextView t, string viewdata) 
    { 
     t.SetText(viewdata, TextView.BufferType.Editable); 

     //adjust the porperties of the textView 
     //t.SetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

     //You have to use Android.Graphics.Color not System.ConsoleColor; 
     t.SetTextColor(Android.Graphics.Color.Blue); 
     t.SetBackgroundColor(Android.Graphics.Color.Black); 
     t.SetPadding(5, 0, 0, 0); 

     tr.SetPadding(0, 1, 0, 1); 
     tr.SetBackgroundColor(Android.Graphics.Color.Black); 

     tr.AddView(t); // add TextView to row. 

    } 
} 

,並沒有顯示出來。任何人都可以知道發生了什麼?

回答

1

您的滾動視圖對象是一切你的容器視圖,但你沒有添加它的對象的看法,以下內容添加到您的GridDataView構造函數的末尾:

AddView(sc); 

後爲我工作。