2016-04-05 2 views
0

Das Relativayout (R.id.relativeLayout1) in activity_main.xml wird durch das Fragmentlayout hotel_search ersetzt. Nach dem Ersetzen des relativen Layouts durch Fragment werden die ursprünglichen Views des relativen Layouts ersetzt sind noch sichtbar. Screenshot enthalten.Android-Views im Relativayout sind auch nach dem Ersetzen durch ein anderes Fragment sichtbar

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    tools:context="com.example.jobinsabu.georgetravels.MainActivity"> 
<android.support.v7.widget.Toolbar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/toolbar" 
     android:background="@android:color/transparent"> 

    </android.support.v7.widget.Toolbar> 
<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/relativeLayout1"> 
<android.support.v4.view.ViewPager android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:layout_marginLeft="30dp" 
    android:layout_marginRight="30dp" 
    android:visibility="invisible" 
    android:id="@+id/viewpager"> 

</android.support.v4.view.ViewPager> 

      <Button 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="40dp" 
       android:id="@+id/homeflight" 
       android:background="@drawable/homeflightcircle" 
       android:layout_gravity="left|center_vertical" 
       android:layout_below="@+id/viewpager" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"> 

      </Button> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="flights" 
       android:textColor="#ffffff" 
       android:layout_below="@+id/homeflight" 
       android:layout_alignRight="@+id/homeflight" 
       android:layout_alignEnd="@+id/homeflight" 
       android:layout_marginRight="4dp" 
       android:layout_marginEnd="4dp" 
       android:layout_marginTop="3dp" 
       android:id="@+id/textView" /> 
      <Button 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="@drawable/homehotelcircle" 
       android:layout_alignTop="@+id/homeflight" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/homehotel" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="hotels" 
       android:textColor="#ffffff" 
       android:layout_alignTop="@+id/textView" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/textView2" /> 
      <Button 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="@drawable/homeholidaycircle" 
       android:layout_alignTop="@+id/homehotel" 
       android:layout_alignRight="@+id/viewpager" 
       android:layout_alignEnd="@+id/viewpager" 
       android:id="@+id/button" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="holidays" 
       android:id="@+id/holidaytext" 
       android:textColor="#ffffff" 
       android:layout_alignTop="@+id/textView2" 
       android:layout_alignLeft="@+id/button" 
       android:layout_alignStart="@+id/button" /> 
</RelativeLayout> 
</RelativeLayout> 

hotel_search.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" 
    android:background="@android:color/transparent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginBottom="60dp" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="30dp" 
     android:background="#60212121"> 

     <TextView 
      android:id="@+id/locationtext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="30dp" 
      android:text="Location" 
      android:textColor="#BDBDBD" 
      android:textSize="15sp" /> 

     <AutoCompleteTextView 
      android:id="@+id/autohotellocation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/locationtext" 
      android:layout_marginLeft="30dp" 
      android:layout_marginTop="20dp" 
      android:background="@android:color/transparent" 
      android:text="Hotel Name/City/Code" 
      android:textColor="#ffffff" /> 

     <View 
      android:id="@+id/view1" 
      android:layout_width="100dp" 
      android:layout_height="0.5dp" 
      android:layout_below="@+id/autohotellocation" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="22dp" 
      android:background="#00838F" 
      android:foregroundGravity="center"></View> 

     <TextView 
      android:id="@+id/checkin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/view1" 
      android:layout_marginLeft="30dp" 
      android:layout_marginTop="10dp" 
      android:text="CheckIn" 
      android:textColor="#BDBDBD" /> 

     <RelativeLayout 
      android:id="@+id/relativeLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:layout_alignLeft="@+id/locationtext" 
      android:layout_alignStart="@+id/locationtext" 
      android:layout_below="@+id/checkin" 
      android:layout_marginLeft="12dp" 
      android:layout_marginStart="12dp"> 

      <TextView 
       android:id="@+id/checkindate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:text="18" 
       android:textColor="#fff" 
       android:textSize="23sp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/checkindate" 
       android:text="Jun 2015" 
       android:textColor="#fff" 
       android:textSize="15sp" /> 
     </RelativeLayout> 

     <View 
      android:id="@+id/view2" 
      android:layout_width="100dp" 
      android:layout_height="0.5dp" 
      android:layout_below="@+id/relativeLayout" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="22dp" 
      android:background="#00838F" 
      android:foregroundGravity="center"></View> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/view2"> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/view2" 
       android:layout_marginTop="30dp"> 

       <RelativeLayout 
        android:id="@+id/roomlayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/transparent"> 


        <TextView 
         android:id="@+id/rooms" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

        <TextView 
         android:id="@+id/adults" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/rooms" 

         android:layout_toEndOf="@+id/button3" 
         android:layout_toRightOf="@+id/button3" 
         android:text="Adults" 
         android:textColor="#BDBDBD" /> 

        <Button 
         android:id="@+id/button3" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_below="@+id/adults" 
         android:layout_marginLeft="18dp" 
         android:layout_marginStart="18dp" 
         android:layout_marginTop="10dp" 
         android:layout_toEndOf="@+id/rooms" 
         android:layout_toRightOf="@+id/rooms" 
         android:background="#90006064" 
         android:text="-" 
         android:textColor="#fff" 
         android:textSize="22sp" /> 

        <Button 
         android:id="@+id/button4" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_alignLeft="@+id/adults" 
         android:layout_alignStart="@+id/adults" 
         android:layout_alignTop="@+id/button3" 
         android:layout_marginLeft="37dp" 

         android:layout_marginStart="37dp" 
         android:background="#90006064" 
         android:text="+" 
         android:textColor="#fff" 
         android:textSize="18sp" /> 

        <TextView 
         android:id="@+id/children" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_above="@+id/button3" 
         android:layout_alignParentEnd="true" 
         android:layout_alignParentRight="true" 
         android:layout_marginEnd="38dp" 
         android:layout_marginRight="38dp" 
         android:text="Children" 
         android:textColor="#BDBDBD" /> 

        <Button 
         android:id="@+id/button6" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_marginEnd="13dp" 

         android:layout_marginRight="13dp" 
         android:background="#90006064" 
         android:text="+" 
         android:textColor="#fff" 
         android:textSize="18sp" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentEnd="true" /> 

        <Button 
         android:id="@+id/button5" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:background="#90006064" 
         android:text="-" 
         android:textColor="#fff" 
         android:textSize="22sp" 
         android:layout_alignParentBottom="true" 
         android:layout_toLeftOf="@+id/button6" 
         android:layout_toStartOf="@+id/button6" 
         android:layout_marginRight="41dp" 
         android:layout_marginEnd="41dp" /> 
       </RelativeLayout> 
      </RelativeLayout> 
     </ScrollView> 

     <TextView 
      android:id="@+id/checkout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignTop="@+id/checkin" 
      android:layout_marginEnd="21dp" 
      android:layout_marginRight="21dp" 
      android:text="CheckOut" 
      android:textColor="#BDBDBD" /> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/checkout" 
      android:layout_alignStart="@+id/checkout" 
      android:layout_alignTop="@+id/relativeLayout" 
      android:id="@+id/relativeLayout2"> 

      <TextView 
       android:id="@+id/checkoutdate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:text="18" 

       android:textColor="#fff" 
       android:textSize="23sp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/checkoutdate" 
       android:text="Jun 2015" 
       android:textColor="#fff" 
       android:textSize="15sp" /> 
     </RelativeLayout> 

    </RelativeLayout> 
</RelativeLayout> 

MainActivity.java:

package com.example.jobinsabu.georgetravels; 

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
     Button button; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button=(Button) findViewById(R.id.homehotel); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       HotelSearch hotelSearch=new HotelSearch(); 
       FragmentTransaction transaction=getSupportFragmentManager().beginTransaction(); 
       transaction.replace(R.id.relativeLayout1,hotelSearch); 
       transaction.commit(); 
      } 
     }); 
    } 
} 

HotelSearch.java:

package com.example.jobinsabu.georgetravels; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by Jobin Sabu on 4/5/2016. 
*/ 
public class HotelSearch extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v=inflater.inflate(R.layout.hotel_search,null,false); 
     return v; 
    } 
} 

enter image description here

+0

Geben Sie dem Fragmentlayout Hintergrundfarbe Weiß. –

+0

Dies liegt an der Transparenz. Fügen Sie den Fragmenten eine andere Hintergrundfarbe hinzu, und sehen Sie sich die Änderungen an. – Stallion

Antwort

0

Geben Sie dem Fragmentlayout Hintergrundfarbe Weiß.


android:background="@android:color/transparent"

mit
android:background="color/white"

0

ersetzen müssen Sie replce() verwenden, wenn Sie Fragment mit einem anderen Fragment in Ihrem Fall ersetzen wollen Sie nur Fragment zu Ihrer Aktivität hinzugefügt, so dass alle Ihre innen einen ersten RelativeLayout definieren neues Fragment verwenden Sie dann einen FrameLayout als Hauptbehälter

1

Fragment funktioniert nicht so. FragmentTransaction.replace wird ein angehängtes Fragment entfernen und das neue dem Layout hinzufügen.

Aber diese ViewPager und Button befinden sich nicht innerhalb eines Fragments, aber es ist fest codiert im Layout von Activity, so dass FragmentTransaction diese Ansichten nicht entfernen kann.

Was ist zu tun?

Lassen Sie diese relativeLayout1 leer und gruppieren Sie diese untergeordneten Ansichten als Fragment. Und dann können Sie sie entfernen, wie Sie möchten.

+0

Ich habe das Relative Layout leer gemacht und alle Ansichten in einem anderen Layout gruppiert, und ich habe das Fragment zum relativen Layout hinzugefügt. Es funktioniert bis jetzt. Aber jetzt, wenn ich den Button klicke das fragment layout und ein anderes layout namens hotel_search müssen das relativelayout ersetzen, aber ich bekomme eine IllegalArgumentException (java.lang.IllegalArgumentException: Keine findung gefunden für id 0x7f0c0051 (com.example.jobinsabu.georgetravels: id/relativeLayout1) für fragment HotelSearch {9a3d93 # 0 id = 0x7f0c0051}) – jobin

Verwandte Themen