2012-10-02 52 views
1

我使用切換按鈕創建了一個活動。我還編寫了處理點擊的代碼。切換按鈕不在應用程序中顯示

但是當我運行該應用程序時,切換按鈕不會顯示在活動屏幕上。

我認爲這可能是一個模擬器的問題,所以我導出它,並嘗試在我的手機上。

但它不顯示在手機上也。

以下是活動佈局文件說明。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/btnStartService" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Start Notifying" /> 

    <EditText 
     android:id="@+id/txtUserMsg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btnStartService" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="82dp" 
     android:ems="10" 
     android:inputType="textMultiLine" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/txtWelcomeMsg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="18dp" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/txtUserMsg" 
     android:layout_centerHorizontal="true" 
     android:text="Message to reply with" /> 

    <Button 
     android:id="@+id/stopNotifying" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/btnStartService" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="40dp" 
     android:text="Stop Notifying" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btnStartService" 
     android:layout_alignLeft="@+id/txtUserMsg" 
     android:layout_marginBottom="29dp" 
     android:text="Caller Notifications" /> 

    <ToggleButton 
     android:id="@+id/tglStartStop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView2" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_toRightOf="@+id/txtWelcomeMsg" 
     android:text="ToggleButton" 
     android:visibility="visible" /> 

</RelativeLayout> 

任何想法,這可能是爲什麼?

+1

請張貼的代碼,你使用切換按鈕。我已經測試了你的xml,它工作正常。這可能是你的代碼中的東西。 –

回答

1

我認爲你有一個概念錯誤,定位與其他部件的時候,你應該這樣來做: 例子:

android:layout_toRightOf="@id/txtWelcomeMsg" 

相反的:

android:layout_toRightOf="@+id/txtWelcomeMsg" 
+0

如果確實幫助你,你應該標記爲正確:) – Daniel

+0

這確實有幫助,但我有一個問題。我沒有在屬性中添加@ + id。它由圖形佈局編輯器完成。這是否意味着佈局編輯器有錯誤? –

1

一般情況下,您只能在ID字段中使用@ + ID。否則使用@id來分配所有的作業。 但是這不應該導致問題,請張貼您使用切換按鈕的代碼。

+1

實際上將@ + id更改爲@id修復了問題,並且切換按鈕開始顯示。但我沒有在屬性中添加+。它由圖形佈局編輯器完成。我想我應該停止使用圖形佈局編輯器。 –

+0

是啊!有時它會弄髒:) –

+0

它總是搞砸了! 到成爲一個滋擾點:( –

相關問題