2016-09-07 41 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>