2016-04-05 19 views
0

將activity_main.xml中的Relativelayout(R.id.relativeLayout1)替換爲片段佈局hotel_search.But將片段替換爲relativelayout後,將相關佈局的原始視圖仍然可見。 包括截圖。即使在用另一個片段替換後,也可以在Relativelayout中看到Android-Views

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

爲背景色設置片段佈局。 –

+0

這是由於透明度。爲片段添加不同的背景顏色並查看更改 – Stallion

回答

0

給背景顏色白色片段佈局。

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


android:background="color/white"

0

必須使用replce()當你想和你剛纔添加片段到你的活動,你的情況另一個片段取代片段這麼定義內的所有第一RelativeLayout新片段然後使用FrameLayout作爲主容器

1

片段不會以這種方式工作。 FragmentTransaction.replace將刪除附加的片段並將新的添加到佈局。

但是那些ViewPager和Button不在片段內,但它被硬編碼放置在Activity的佈局中,所以FragmentTransaction不能刪除這些視圖。

怎麼辦?

將relativeLayout1留空並將這些子視圖作爲片段分組。然後你可以隨意刪除它們。

+0

我已將相關佈局設置爲空並將所有視圖分組到另一個佈局中,並且我已將片段添加到相關佈局中。直到現在,它仍可以正常工作。但現在,當單擊按鈕時片段佈局稱爲hotel_search的其他佈局必須替換relativelayout,但我得到一個IllegalArgumentException(java.lang.IllegalArgumentException:沒有爲ID 0x7f0c0051(com.example.jobinsabu.georgetravels:id/relativeLayout1)找到片段HotelSearch {9a3d93# 0 id = 0x7f0c0051}) – jobin

相關問題