2016-04-03 113 views
1

我需要在活動中將RelativeLayout塊設置爲VISIBLE。但我似乎無法給RelativeLayout分配給一個變量,像這樣:android:如何使用XML中定義的id初始化/膨脹一個RelativeLayout?

Relative Layout alarmSetBlock = (RelativeLayout) findViewById(R.id.sleep_text_block); 

alarmSetBlock分配null代替。

我想我應該使用類似inflater.inflate的東西,但不知道如何在碎片之外使用它。

請告訴我解決這個問題的最簡單/最好的方法。

佈局的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp"> 
     <Button 
      android:id="@+id/alarm_set_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:paddingLeft="20dp" 
      android:paddingRight="20dp" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp" 
      android:background="@color/mediumGray" 
      android:text="SET ALARM" 
      android:onClick="showTimePickerDialog"/> 
    </RelativeLayout> 


    <RelativeLayout 
     android:id="@+id/sleep_text_block" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:visibility="gone"> 
     <TextView 
      android:id="@+id/sleep_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="50dp" 
      android:layout_centerHorizontal="true" 
      android:textSize="20sp" 
      android:text="Alarm set to :"/> 

     <TextView 
      android:id="@+id/alarm_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/sleep_text" 
      android:textColor="@color/colorAccent" 
      android:textSize="36sp" /> 
    </RelativeLayout> 

</LinearLayout> 

RelativeLayout我想初始化是第二個佈局。

這是上述佈局的片段文件。我試圖初始化RelativeLayout裏面的活動java文件,它包含這個片段。

public class SleepSetFragment extends Fragment { 

    public static SleepSetFragment newInstance() 
    { 
     SleepSetFragment fragment = new SleepSetFragment(); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.fragment_sleep_set, container, false); 
    } 
} 
+0

您可以添加活動的XML? –

+0

這應該工作,你使用多個佈局文件夾?確保這些ID也在每個XML中設置。 –

+0

我所有的佈局都在我的res/layout文件夾中。我如何在每個XML中設置ID?如果你的意思是使用'android:id'來定義ID,我已經在這裏完成了。 –

回答

1

你忘了在膨脹onCreateView(在XML佈局)這樣

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

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

      RelativeLayout alarmSetBlock = (RelativeLayout) view.findViewById(R.id.sleep_text_block); 
alarmSetBlock.setVisibility(Visibility.VISIBLE); 

      return view; 
     } 
+0

我在哪裏設置我的'RelativeLayout'的可見性?我應該在片段'onCreateView()'方法本身以及它所連接的'SharedPreference'邏輯中執行嗎? –

+0

正好在RelativeLayout alarmSetBlock行的下方 –

+0

您可以查看,因爲片段和活動具有不同的生命週期。 –

1

您試圖從活動中獲取片段中的視圖。 對於這種情況,您不能使用findViewById()。相反,你應該從你的片段中公開一個方法來使用這個特定的視圖。但請確保您的片段在調用之前充氣並添加到活動中。

下面是一個例子:https://stackoverflow.com/a/17023533/1395437

+0

'RelativeLayout'是在一個片段文件,它有onCreateView() –

+0

你試圖從片段的活動或一個視圖你活動中的片段? –

+0

嘗試獲取活動內部片段的視圖。 –