在monodroid上自定义复合控制

时间:2012-05-16 16:50:48

标签: android controls xamarin.android customization

我试图创建自己的Customize Compound Control,然后我开始在Activity上开发它,然后我将相同的代码放在一个扩展自LinearLayout的类中,如Android文档中所示。这是我的Activity的代码,其中我的实例是我自己的自定义控件:

[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 个答案:

答案 0 :(得分:1)

你的scrollview对象是所有内容的容器视图,但你没有添加对象的视图,将以下内容添加到GridDataView构造函数的末尾:

AddView(sc);
之后

为我工作。