我想打开一个 Fragment 并想膨胀 fragment 所在的 View 。这可能吗? 我搜索了这些问题:
- error inflating class fragment fragment did not create a view ;
- calling fragment from activity ;
- how to open specific fragment from other activity onclick ;
- open fragment from activity ;
- how do i prevent overlapping in android ;
我找不到我的答案或者我忽略了它。是否有可能在 onclick 之后打开我的 fragment 时,我的布局将按钮(beetInfosButton)推到我的 fragment 下方(封装在 ScrollView 中)所以我的 fragment 不重叠?我必须使用其他布局而不是 RelativeLayout 吗?或者这是不可能的。希望有人能理解我想要什么。提前致谢
这是 Activity 代码。
public class InfoSeite extends AppCompatActivity implements BodenSeite.OnFragmentInteractionListener {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_seite);
public void buttonBodenInfos(View view){
getFragmentManager().beginTransaction().add(R.id.fragment_container,new BodenSeite()).commit();
}
和 Activity 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#2fb215"
android:id="@+id/infoSeite">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bodenInfosString"
android:id="@+id/bodenInfosButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:onClick="buttonBodenInfos"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView2"
android:layout_toEndOf="@+id/bodenInfosButton"
android:layout_below="@+id/bodenInfosButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/beetInfosString"
android:id="@+id/beetInfosButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="buttonBeetInfos" />
</RelativeLayout>
以及 fragment XML 的示例。
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2fb215"
android:columnOrderPreserved="true"
android:tag="BodenFragment"
android:id="@+id/bodenFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bodenArtenString"
android:id="@+id/bodenSeiteUeberschrift"
android:layout_row="0"
android:layout_column="0"
android:textSize="40dp"
android:textAlignment="center" />
最佳答案
由于您使用的是 RelativeLayout
,因此您可以选择将 Views
相对于彼此或它们的父级“粘贴”。
伪代码:
<RelativeLayout>
<Button />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<Button />
<RelativeLayout>
在你的 onClick
中:
scrollView.setVisibility(View.GONE); //View.VISIBLE, when you close the fragment
关于java - 从 Activity 中打开 fragment 并膨胀 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38198958/