2012-08-06 77 views
3

今天我發生了一件奇怪的事情,並引發了數小時的挫折感和憤怒管理問題。將按鈕從相對佈局改爲線性佈局奇怪的副作用

我有一個可用的Android幻燈片應用程序。但是,我對這些按鈕(它們處於相對佈局)感到不滿,並將它們移動到線性佈局。但是,在運行我的應用程序時,按鈕的行爲會被交換(即Button 1執行Button 2的工作,反之亦然)。我可以通過更改其中一個按鈕的ID來解決這個問題,而不是所有的(不起作用)。與此相關的是,當我切換方向時,我的應用程序開始崩潰。之前我有過相對佈局時它曾經工作過。 (我正在使用處理程序更改幻燈片的用戶界面)

我無法解決此問題,因此我恢復爲相對佈局,但我想知道這可能是如何發生的。導致問題

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



    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:contentDescription="@string/imageView" 
     android:scaleType="centerInside" 
     android:src="@drawable/image1" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 



     <ImageButton 
      android:id="@+id/startStop" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:contentDescription="@string/startStop" 
      android:src="@drawable/ic_menu_slideshow" /> 

     <ImageButton 
      android:id="@+id/next" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="false" 
      android:layout_toRightOf="@+id/startStop" 
      android:contentDescription="@string/next" 
      android:src="@drawable/ic_media_next" /> 

     <ImageButton 
      android:id="@+id/previous" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="false" 
      android:layout_toLeftOf="@+id/startStop" 
      android:contentDescription="@string/previous" 
      android:src="@drawable/ic_media_previous" /> 

</RelativeLayout> 

代碼:

,工程佈局

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



<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_centerInParent="true" 
    android:contentDescription="@string/imageView" 
    android:scaleType="centerInside" 
    android:src="@drawable/image1" /> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:background="#30eee9e9" > 

      <ImageButton 
     android:id="@+id/previous" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/previous" 
     android:src="@drawable/ic_media_previous" /> 

    <ImageButton 
     android:id="@+id/bla" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/startStop" 
     android:src="@drawable/ic_menu_slideshow" /> 

    <ImageButton 
     android:id="@+id/next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/next" 
     android:src="@drawable/ic_media_next" /> 

+4

你可以發佈你的佈局xml和代碼,你處理按鈕點擊? – vipsy 2012-08-06 16:27:32

+1

您必須使用相對佈局的LeftOff或RightOff功能(作爲父母),是這樣嗎? – 2012-08-06 16:41:26

+0

我猜你應用程序崩潰後,由於ClassCast或NullPointer異常的方向更改。你有不同的肖像和風景佈局嗎?如果是這樣,你需要更新兩個佈局。但沒有相關的**代碼**和** logcat **錯誤,我們只能猜測。 – Sam 2012-08-06 17:03:09

回答

-2

我有同樣的問題,但與相對佈局父&線性佈局爲子佈局我使用了四個圖像按鈕

  • PLAY
  • SCORE
  • 幫助
  • 退出

和按鈕的功能被轉移到一個低於下一即當我按下PLAY什麼都沒有發生時,按下積分遊戲開始玩點擊時HELP給我看分數等等。我整整一個晚上解決問題,但沒有發生任何事情,我關閉了大壩試圖觀看電影,但有一段時間我看到它,我又開始了Eclipse,它工作得很好的魔力。不知道但重建&乾淨已經解決了我的問題,因爲DeeV說

+0

這不是一個真正的答案,而是Deev的評論對你有幫助的確認。如果你發現Deev的評論很有幫助,你會想要簡單地讚揚它! – 2013-09-07 03:11:23