2011-02-09 112 views
0

我有一個奇怪的問題,在屏幕上顯示的按鈕,但我不能點擊。這並不是onClick事件不會觸發,它似乎在物理上不會對點擊產生反應,就好像它在頂部有一個透明層。不可能點擊一個按鈕

主要Activity它是建立在:

public class MusicList extends Activity implements OnClickListener { 
... 
@Override 
    public void onCreate(Bundle bundle) 
    { 
     super.onCreate(bundle);  

     setContentView(R.layout.mymusic); 
     this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost); 
     this.myTabHost.setup(); 

     TabSpec ts = myTabHost.newTabSpec("TAB_TAG_1"); 

     ts.setIndicator("Media Item",getResources().getDrawable(R.drawable.music));    

     ts.setContent(R.id.media_item_tab); 

     try { 
      relativeLayout = (RelativeLayout) myTabHost.findViewById(android.R.id.tabcontent).findViewById(R.id.media_item_tab); 
     } catch (NullPointerException e) { 
      Log.e(this.getClass().getSimpleName(),"Layout is not correct",e); 
     } 

      Button button = (Button)relativeLayout.findViewById(R.id.play_button); 
      // button.setClickable(true); 
      button.setOnClickListener(this); 
        myTabHost.addTab(ts); 
    } 
    @Override 
    public void onClick(View v) { 
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(itemData.get("ITEM_URL")))); 
    } 
} 

的佈局,這是完美的顯示在屏幕上是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/th_set_menu_tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingTop="65px"> 

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/media_item_tab" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <TextView 
       android:id="@+id/media_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" 
       android:textColor="#40A5D9" 
       android:gravity="center" 
       android:layout_alignParentTop="true" 
       android:singleLine="true" 
       android:ellipsize="marquee" 
       android:text="Title" 
       > 
       </TextView> 
       <ImageView 
       android:id="@+id/media_cover" 
       android:layout_width="fill_parent" 
       android:layout_height="180px" 
       android:gravity="center" 
       android:layout_below="@+id/media_title" 
       android:src="@drawable/geniocover"    
       > 
       </ImageView> 
       <TextView 
       android:id="@+id/media_author" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="20sp" 
       android:textColor="#86CCF3" 
       android:gravity="center" 
       android:singleLine="true" 
       android:ellipsize="marquee" 
       android:layout_below="@+id/media_cover" 
       android:text="Author" 
       > 
       </TextView> 
       <TextView 
       android:id="@+id/media_album" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:textColor="#DAFFFF" 
       android:gravity="center" 
       android:singleLine="true" 
       android:ellipsize="marquee" 
       android:layout_below="@+id/media_author"  
       android:text="Album (Year)" 
       > 
       </TextView> 
       <Button 
       android:id="@+id/play_button" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Play" 
       android:textColor="#003983" 
       android:textStyle="bold" 
       android:textSize="20sp" 
       android:gravity="center" 
       android:layout_below="@+id/media_album" 
       android:clickable="true"  
       > 
       </Button> 
      </RelativeLayout> 
      <ListView 
      android:id = "@+id/tablist" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 
    </FrameLayout> 
</TabHost> 

有什麼建議?

謝謝!

+0

我很好奇:爲什麼在所有元素都低於前一個時使用RelativeLayout - 具有orientation = vertical的LinearLayout可以做到這一點? – 2011-02-09 16:35:31

+0

那麼,當前的佈局只是我們正在研究的一個模型,所以它從第一個狀態(當它確實是一個RelativeLayout)遭受了一些變化。爲了排除這個問題的根源,我會嘗試改變這一點。 – 2011-02-09 16:46:21

+0

你說*這不是說onClick事件不會觸發*,但我在評論中看到你也說*它永遠不會進入*。我很困惑。 – Nanne 2011-02-09 16:52:49

回答

1

你有一個FrameLayout有兩個孩子,當它只是想主辦一個。 ListView最後被添加,最終將排在最前面,可能會收到您的所有輸入。

0

我認爲問題是由您的tablayout的複雜結構造成的。

通常,我使用單獨的Activity來控制TabLayout,並將不同的活動作爲內容放入到tabhost(tabSpec.setContent(Intent intent))中。

其實這應該可以解決您的問題。

順便說一句,避免使用RelativeLayout,你可以使用LinearLayout。