1
我將我的應用程序從Windows Phone 8移植到Android - 我需要創建一些自定義UI控件。我試圖創建一個XML佈局,在它內部創建一個LinearLayout作爲控件,然後動態地添加它(根據用戶的需求) - 但那不起作用。我怎樣才能完成這個儘可能簡單的方式?在Xamarin中創建自定義控件
這是我試過的代碼:
MainActivity.cs:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Knowledge_Organizer
{
[Activity (Label = "Knowledge_Organizer", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Import the items from resources
LinearLayout layout = FindViewById<LinearLayout> (Resource.Id.rootLayout);
// Add the tile
Resource.Layout.Tile tileRoot = FindViewById<LinearLayout> (Resource.Layout.Tile);
var tile = (Resource.Id.projectTile);
layout.AddView (tile);
}
}
}
主要佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/rootLayout" />
瓷磚:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="290dp"
android:layout_height="120dp"
android:id="@+id/linearLayout1"
android:background="#ff2e4653">
<TextView
android:text="Project"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/projectTile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="0.0dp" />
</LinearLayout>
</LinearLayout>
感謝很多提前!
親切的問候, 埃裏克
謝謝,它的工作 - 但我怎麼能修改圖塊中的一個特定的控制?請記住,根據用戶的需求動態地添加磁貼 – Erik
我已經用如何設置文本的示例更新了答案。如果答案適合您,您應該將其標記爲正確。這就是這個網站的工作原理。 – DaveDev
剛剛做了,非常感謝您的幫助! – Erik