2015-02-11 16 views
0

我的問題是,我在主RelativeLayout中的ScrollView內的LinearLayout中有多個RelativeLayouts。當試圖獲取片段類中的ID時,我一直給我提供錯誤。從嵌套佈局檢索Id's

現在我已經看了看四周的答案爲這個約2天,只遇到使用

<include name="some_name" layout="@layout/some_layout"/> 

跨越一個想法,它並沒有真正幫助我與我的應用程序。我正在嘗試開發一個日曆周視圖,我已經有了數字工作和左邊的時間。

這裏是我的xml文件:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:id="@+id/day_labels_linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:orientation="horizontal"> 
     <!-- not important but this holds the numbers up top --> 
    </LinearLayout> 
    <ScrollView 
     android:id="@+id/calendar_scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@+id/day_labels_linear_layout" 
     android:fadingEdge="none" 
     android:overScrollMode="never" 
     android:scrollbars = "none"> 

     <!-- used to space each day with the day_labels_linear_layout weights --> 
     <LinearLayout 
      android:id="@+id/calendar_spacer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <!--This has all of the hours, I just didn't include them 
       because it would take up so much room--> 
      <RelativeLayout 
       android:id="@+id/hours_relative_layout" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2.5" 
       android:background="@color/scheduler_background" > 

       <TextView 
        android:id="@+id/time_12_am" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:paddingBottom="25dp" 
        android:paddingTop="25dp" 
        android:text="@string/text_time_12_am" /> 

       <TextView 
        android:id="@+id/time_1_am" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/time_12_am" 
        android:layout_centerHorizontal="true" 
        android:paddingBottom="25dp" 
        android:paddingTop="25dp" 
        android:text="@string/text_time_1_am" /> 

       <TextView 
        android:id="@+id/time_2_am" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/time_1_am" 
        android:layout_centerHorizontal="true" 
        android:paddingBottom="25dp" 
        android:paddingTop="25dp" 
        android:text="@string/text_time_2_am" /> 

      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/sunday_relative_layout" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2" > 

       <View 
        android:id="@+id/divider_sunday_left" 
        android:layout_width="1dp" 
        android:layout_height="match_parent" 
        android:layout_alignParentLeft="true" 
        android:background="@color/divider_scheduler" /> 

       <Button 
        android:id="@+id/sunday_day" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_centerInParent="true" 
        android:background="@color/transparent" /> 

       <View 
        android:id="@+id/divider_sunday_right" 
        android:layout_width="1dp" 
        android:layout_height="match_parent" 
        android:layout_alignParentRight="true" 
        android:background="@color/divider_scheduler" /> 
      </RelativeLayout> 

      <!-- there is a Relative layout for each day--> 

     </LinearLayout>   
    </ScrollView> 
</RelativeLayout> 

所以,當我嘗試訪問的ID,這是我所得到的:

RelativeLayout sundayRL; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState){ 
    View view = inflater.inflate(R.layout.fragment_scheduler, null); 

    sundayRL = (RelativeLayout) view.findViewById(R.id.sunday_relative_layout); 


} 

它給我的錯誤:sunday_relative_layout不能得到解決或者是不是一個領域,我添加了一個按鈕,看看我是否可以得到這個ID,併發生相同的結果。然而,我能夠找到頂部LinearLayout中每日數字和日期名稱的id。

有沒有人有更好的解決方案呢?我試圖在用戶點擊視圖時註冊,以便我可以將它們發送到計劃頁面。

編輯: 因此改變空集裝箱讓我找到我的IDS

View view = inflater.inflate(R.layout.fragment_scheduler, container); 

但它與地圖碎片造成的另一個問題的其他片段中的一個說我沒有正確的API密鑰它所做的。

+0

只是檢查,是你貼'fragment_scheduler'的佈局代碼? – 2015-02-11 21:59:16

+0

是的,那是我的xml文件 – 2015-02-11 22:00:01

+0

爲什麼不使用GridView來顯示相同​​類型的項目? – 2015-02-11 22:04:40

回答

0

如果您有嵌套佈局 如:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout > 
    <RelativeLayout 
     android:id="@+id/rl2> 
     <Button 
      android:id="@+id/getStarted" 
      /> 
    </RelativeLayout> 

</RelativeLayout> 

而你要在嵌套一個按下按鈕。 在嵌套佈局上添加ID。

和使用的onCreate代碼()

View view = (View) findViewById(R.id.rl2); 
Button button = (Button) view.findViewById(R.id.getStarted);