我的content_main.xml中有一个cardview和一个按钮,如果用户按下按钮,它应该显示片段(命名为radio fragment),当我这样做时,cardview仍然显示在无线电片段中(片段名称) )
当用户按下按钮时,我想隐藏cardview和按钮并显示片段
这是我的内容主要
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.CardView
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:cardElevation="10dp" >
</android.support.v7.widget.CardView>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这里是fragment_radio.xml
<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"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.Radio">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center" />
</RelativeLayout>
这是主要活动中的按钮功能
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Radio radio=new Radio();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.Relative, radio,radio.getTag()).commit();
}
});
}
任何人都可以帮助我?
答案 0 :(得分:3)
使用FrameLayout作为根布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.Radio">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat" />
</RelativeLayout>
</FrameLayout>
然后使用此ID替换您的片段:R.id.container
答案 1 :(得分:1)
尝试为Relativelayout
添加背景:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.MainActivity"
tools:showIn="@layout/activity_main">
如果这不起作用; 尝试:
manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
android.R.id.content
为您提供了视图的根元素,
答案 2 :(得分:1)
正如其他人所指出的,你不会替换容器内的视图。
replace()
的文档说:
替换添加到容器的现有片段。这与使用相同的containerViewId添加的所有当前添加的片段调用
remove(Fragment)
基本相同,然后使用此处给出的相同参数调用add(int, Fragment, String)
。
我不认为您正在替换现有的片段。
您可以隐藏CardView
和Button
,或者在片段上设置背景也可以解决此问题。
答案 3 :(得分:1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<select class="person-1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="person-2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<p>Add 1: <span class="addition-1"></span></p>
<p>Add 2: <span class="addition-2"></span></p>
<p>Add 1+2 : <span class="addition"></span> </p>
</body>
</html>
和CardView
正在显示,因为它们都高于Button
。
因此,当您在RelativeLayout
上设置片段时,它会置于这些视图下。
一种解决方案是覆盖布局中的RelativeLayout
和cardview
,并在显示片段时隐藏布局,并在反向过程中取消隐藏。