2016-01-06 14 views
0

我是編程新手,我試圖製作一個帶有2個選項卡的操作欄,我需要的是,單擊第一個選項卡時出現2個按鈕,我試圖使用SetContentView按鈕);但這只是表明一個按鈕全屏c#Xamarin在c#中更改UI#

問題:我需要它來顯示2個按鈕,而不是1

代碼:

private void Pudisoo_TabSelected1(object sender, ActionBar.TabEventArgs e) 
    { 
     Button btnon = new Button(this); 
     btnon.Text = "ON"; 
     btnon.Click += Btnon_Click1; 
     SetContentView(btnon); 

     Button btnoff = new Button(this); //<--- I can only see this button 
     btnoff.Click += Btnoff_Click; 
     SetContentView(btnoff);  //<--- Because of this 

    } 
+0

你在哪裏試圖讓按鈕出現?你可以發佈你的代碼活動? – dylansturg

回答

1

您還沒有指定您希望這些按鈕可以顯示用戶;

SetContentView方法用於通過'膨脹'佈局文件來創建活動的視圖。您應該通過此佈局文件而不是代碼創建大部分視圖(例如按鈕),特別是如果您不熟悉C#,Android或一般編程時。

下面是一些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"> 
    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnOff" /> 
    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnOn" /> 
</LinearLayout> 

而且你應該在你的Pudisoo_TabSelected1方法使用的代碼:儘管這整個的做法實際上是不正確

private void Pudisoo_TabSelected1(object sender, ActionBar.TabEventArgs e) 
{ 
    SetContentView(Resource.Layout.filenamehere); 
    Button btnOn = FindViewById<Button> (Resource.Id.btnOn); 
    Button btnOff = FindViewById<Button> (Resource.Id.btnOff); 
    btnOff.Click += Btnoff_Click; 
    btnOn.Click += Btnon_Click1; 
} 

(你應該使用碎片來達到這種效果),它至少會做你想做的事情,並且可以作爲一個起點,而你變得更加舒適。

+0

非常感謝您先生,您剛剛解決了我所有的問題,並解答了我的所有問題。你的代碼工作完美,並做了我想做的事情,我不在乎這是否是一種不完整的方法,因爲我通常不會進行android開發,而只需要使用幾個按鈕和製表符創建一個小應用程序,再次非常感謝你。 – Ingmar05

+0

我還想指出,在你的第二個例子中,第5行需要有「btnoff」而不是「btnOff」 – Ingmar05