2016-09-07 89 views
0

這是我activity_main.xml中如何訪問片段內activity_main.xml中的線性佈局

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

    <LinearLayout 
     android:id="@+id/banner_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/contact_card_line_color" 
     android:gravity="center_horizontal" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/no_internet_connection" 
      android:textColor="@color/contact_card_initial_color" 
      android:textSize="@dimen/banner_text_size" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/fragment_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/banner_layout"> 

    </FrameLayout> 


    </RelativeLayout> 

在我的主要活動,我做

setContentView(R.layout.activity_launch_view); 

    fragment.setArguments(getIntent().getExtras()); 
      getFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).commit(); 

在我的片段,

1)默認情況下無法看到沒有互聯網連接的文本 2)我可以訪問它們,沒有運行時錯誤,但看不到文本。

+0

背後的原因是你放置的TextView的FrameLayout之前,這樣的的FrameLayout隱藏框架佈局後textView.To解決這個問題的地方TextView的。 – Vishwa

+0

使用接口來處理來自片段的線性佈局的任何事件 –

回答

0

由於它是相對佈局,因此您的匹配父級framelayout與您的textview重疊,因此您需要將其更改爲linearlayout或將layout_below標記用於橫幅佈局。

0

使用此代碼

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

     <LinearLayout 
      android:id="@+id/banner_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/contact_card_line_color" 
      android:gravity="center_horizontal" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/no_internet_connection" 
       android:textColor="@color/contact_card_initial_color" 
       android:textSize="@dimen/banner_text_size" /> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/fragment_holder" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/banner_layout"> 

     </FrameLayout> 


     </LinearLayout>