我正在尝试在xamarin android.I中开发一个TreeView,然后按照Link来开发下面的示例。
我不知道如何更改TreeNode的前景色,即parent和child1,它们是Linear Layout的一部分。
我可以更改TreeItems的颜色,因为我在TextView中显示TreeItems。
非常感谢任何帮助。
我的MainActivity.cs:
namespace XamarinTreeView
{
[Activity(Label = "XamarinTreeView", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//create root
TreeNode root = TreeNode.InvokeRoot();
TreeNode parent = new TreeNode("parent2");
///Parent2 root children
TreeNode child1 = new TreeNode("Child1");
TreeItem item = new TreeItem() { text = " Level1" };
TreeNode child10 = new TreeNode(item).SetViewHolder(new MyHolder(this));
child1.AddChild(child10);
TreeItem item1 = new TreeItem() { text = " Level2" };
TreeNode child11 = new TreeNode(item1).SetViewHolder(new MyHolder(this));
child10.AddChild(child11);
TreeItem item2 = new TreeItem() { text = " Level2-1" };
TreeNode child111_1 = new TreeNode(item2).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_1);
TreeItem item3 = new TreeItem() { text = " Level2-2" };
TreeNode child111_2 = new TreeNode(item3).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_2);
///
parent.AddChildren(child1);
root.AddChild(parent);
AndroidTreeView atv = new AndroidTreeView(this, root);
LinearLayout rootlayout = FindViewById<LinearLayout>(Resource.Id.rootlayout);
rootlayout.AddView(atv.View);
rootlayout.Invalidate();
}
}
public class TreeItem : Java.Lang.Object
{
public string text;
}
}
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootlayout">
</LinearLayout>
答案 0 :(得分:2)
创建一个新类来表示TreeNode,就像您对TreeItem一样。 像TreeItem(MyHolder.cs)一样创建一个新的持有者。 像TreeItem(itemview.axml)
一样创建一个新的布局MyNodeHolder.cs
public class MyNodeHolder : TreeNode.BaseNodeViewHolder
{
private Context mcontext;
public MyNodeHolder(Context context) : base(context)
{
mcontext = context;
}
public override View CreateNodeView(TreeNode p0, Java.Lang.Object p1)
{
var inflater = LayoutInflater.FromContext(mcontext);
var view = inflater.Inflate(Resource.Layout.nodeview, null, false);
TextView tv = view.FindViewById<TextView>(Resource.Id.nodetv);
var item = p1 as TreeNodeItem;
tv.Text = item.text;
return view;
}
}
nodeview.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
<TextView
android:id="@+id/nodetv"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout>
在活动中创建您的节点:
streetNode.AddChild(streetNumberNode.SetViewHolder(new MyNodeHolder(this)));
我希望它有所帮助。