【问题标题】:open fragment from activity and inflate the view从活动中打开片段并膨胀视图
【发布时间】:2016-07-05 17:43:21
【问题描述】:

我想打开一个 Fragment 并想放大该 Fragment 所在的视图。这可能吗? 我搜索了这些问题:

  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;

我找不到我的答案,或者我忽略了它。是否有可能在我的布局按下我的片段下方的按钮(beetInfosButton)(封装在滚动视图中)之后打开我的片段时,我的片段不会重叠?我是否必须使用其他布局而不是RelativeLayout?或者这是不可能的。希望有人能理解我想要什么。提前感谢
这是活动代码。

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>

以及片段 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" />

【问题讨论】:

  • 如果我正确理解了您的问题:由于您的ScrollView 被定义为低于您的Button,因此它将始终位于ScrollView 的顶部(因此您的FrameLayout)。您可以更改您的activity_main.xml,使您的Button 将“消失”,或者您可以自己处理ButtonVisibility,因此在buttonBodenInfos 方法中,调用view.setVisibility(View.GONE)。每当您关闭 Fragment 时,您都需要将其设置为 View.VISIBLE
  • 谢谢。但是就没有别的办法了吗?通过将按钮移到下方或其他什么?我必须让它们不可见吗?如果没有其他可能,我会试试这个。
  • 我没有正确阅读您的问题。 ^^'我会添加一个答案。

标签: java android android-layout android-fragments


【解决方案1】:

由于您使用的是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

【讨论】:

  • 啊,是的。我只用代码就得到了上面的答案,并且我已经在 Layout_above 和下面尝试了这些选项。但这并没有达到我想要的效果。布局或按钮仍保留在同一位置。
  • 我假设你是德国人 - 也许在这里用德语评论解释你的问题,所以我可能会理解你;)
  • :D Ich möchte, dass nach dem des Buttons buttonBodenInfos(), welches das Fragment öffnet, sich die Buttons die sich in der View derzeit under dem buttonBodenInfos() befinden, Platz machen für das sich öffnende片段。
  • 对于其他所有人:当片段自行打开时,Buttons 应该会消失。对于@Adije:据我所知,最好使用两个Fragments - 一个使用Buttons,一个使用ScrollView。如果你想保留你的布局,你可以尝试将width和'height'设置为match_parent(你的ScrollView),它的Visibility来自View.GONE/View.VISIBLE
  • :D 我认为我无法正确陈述我的问题或我想要的效果。我不希望 Buttons 消失。我希望它们仍然显示,但如果我的 Fragment 打开,它们必须在下方,以便它们不会重叠。但我认为这是不可能的,因为我所有的答案都回来隐藏其他布局/按钮。我将把这个答案说成是正确的,因为我会选择隐藏我的 LayoutsButtons 或使用两个 Fragments 的解决方案。取决于哪个更适合我。谢谢
【解决方案2】:

试试下面的代码:

<?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_below="@+id/scrollView2"
        android:onClick="buttonBeetInfos" />

</RelativeLayout>

【讨论】:

  • 感谢您的意见。我尝试了您的代码,但没有成功。但是我通过尝试和错误了解了 layout_above 和 layout_below 的对齐方式。但这对我的问题没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多