2014-12-07 63 views
0

我需要以編程方式從RelativeLayout更改背景色。如何識別活動中的RelativeLayout

在XML文件中我已經做到了這一點:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/milayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/negro" 
    android:orientation="horizontal" 
    android:padding="5dip" 
    > 

然後在活動文件,我想聲明的佈局改變以後的RelativeLayout的背景顏色,就像我在其他帖子看到在這裏。

RelativeLayout MyLayout = (RelativeLayout) findViewById(R.layout.milayout); 

但我到達那裏的錯誤:「在類型創建領域milayout‘佈局’」

我在做什麼錯?

回答

1

看看代碼

RelativeLayout的MyLayout =(RelativeLayout的)findViewById(R 佈局 .milayout。);

改變這種

RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.milayout); 
+0

謝謝你,但現在的錯誤在於對findViewById,它說:「創建方法findViewById(int)的」 – novato 2014-12-07 00:34:56

+0

發表您的活動課,讓我從頂部的OnCreate @諾瓦託看 – Elltz 2014-12-07 00:37:13

+0

沒有必要Elitz,正確的代碼是:RelativeLayout myLayout =(RelativeLayout)itemView.findViewById(R.id.milayout);現在它的工作原理,謝謝你在任何情況下 – novato 2014-12-07 00:42:35