java - 从 Activity 中打开 fragment 并膨胀 View

标签 java android android-layout android-fragments

我想打开一个 Fragment 并想膨胀 fragment 所在的 View 。这可能吗? 我搜索了这些问题:

  1. error inflating class fragment fragment did not create a view ;
  2. calling fragment from activity ;
  3. how to open specific fragment from other activity onclick ;
  4. open fragment from activity ;
  5. 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/

相关文章:

Java反序列化JSON,其中变量名为 "class"

java - 使用非对称加密(公共(public)私钥 Enc)时出现 IllegalBlockSize 异常

java - 由于 VT-x 禁用,无法在 Android Studio 中运行代码

android - 如何在收到消息时启动 Activity ,即使应用已死(滑动)?

java - 验证错误 : Cannot inherit from final class

java - Google Firebase 方法 onDataChange() 未调用

android - 如何通过蓝牙列出来自 Android 的远程文件?

android - 哪个更快?来自 xml 或代码的布局

android - 改变 Material 图标的颜色?

android - Bottom Sheet 不出现,留在 Android 中的 Fragment 后面