java - 以编程方式将布局添加到 fragment

标签 java android layout android-fragments

简介 我想根据用户从抽屉导航中的选择获得不同的 fragment 。对于抽屉导航中的每个项目,都有一个应该调用的不同站点。我想通过 fragment 来实现这一点。 通过在 onCreate 方法中执行以下操作,将首先显示主 fragment

if ( savedInstanceState == null ) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment fragment = MainFragment.passList(hashMap);
    ft.replace(R.id.content_frame, new MainFragment());
    ft.commit();
}

正如您所看到的,我将数据( HashMap )传递给 fragment 的方法,该方法工作得很好。然后数据被放入字符串数组中。这是可行的,并且数据也可以在 fragment 的 onCreate 方法中使用。

这是主要 Activity 的 xml

    <android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/drawer_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

 <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:choiceMode="singleChoice"
       android:divider="@android:color/darker_gray"
       android:dividerHeight="0.1dp" />

</android.support.v4.widget.DrawerLayout>

这是主 fragment 的 XML

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRelativeLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?android:attr/activatedBackgroundIndicator"
   android:gravity="center_vertical"
   android:paddingRight="10dp"
   >
<ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.03" >

        <LinearLayout
            android:id="@+id/mainListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
    </RelativeLayout>

我现在想要实现的是在 ScrollView 中显示数据。但我的问题是,如何填充它以及如何初始化它?

通常我会像这样创建它们 - 因为 findViewById 在 Fragment 中不起作用(必须自己编写方法)

ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout ( getActivity() );

sv.addView(ll); 
ll.addView(SAMPLETEXTVIEW);

View mainView = inflater
            .inflate(R.layout.main_fragment, container, false);
    return mainView;

但这只会让我看到一个空白屏幕,并且没有添加任何实际文本。我怎样才能实现这个目标?数据就在那里 - 我知道,因为它是用 System.out.println 打印出来的 - 我只需要一种显示它的方法。提前谢谢!

编辑

当我这样做的时候

View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 

我只得到一个 IllegalStateException,ScrollView 只能承载一个直接子项...但我不明白。线性布局是唯一的直接子元素。为什么会出现问题?

这是完整的onCreateView方法

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
    source      = new TextView[hashMapSize];


    for (int i = 0; i < hashMapSize; i++) {

        source[i]       = new TextView(getActivity());
source      [i].setText( "Source: "     + sourceArr     [i] );
ll.addView(source[i]

    }
    return rootView;
}

最佳答案

你得到IllegalStateException的原因是因为你膨胀了你的R.layout.main_fragment,它已经包含了你的ScrollView,它已经有一个直接子(在布局xml中声明) )。

您不应该使用 sv.addView(ll) 添加代码,它已经存在了。您所需要做的就是获取引用,就像使用 findViewById 所做的那样。

删除sv.addView(ll);,你应该没问题。

关于java - 以编程方式将布局添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19296113/

相关文章:

android - 如何防止警报对话框被后退按钮关闭

android - 将参数从 Activity 传递到 fragment

android - 离线获取位置

java - 自定义适配器 View (带图片)

css - 如何在固定宽度元素上设置全屏宽度背景?

java - 系统属性的 Spring @value 给出 null

java - React-native 构建错误 Android - java.lang.UnsupportedClassVersionError : com/android/build/gradle/AppPlugin : Unsupported major. 次要版本 52.0

java - 框架中的自定义布局

java - 不同 View 中具有相同 ID 的两个按钮

java - 尝试为文本区域创建滚动 Pane