动态地将视图添加到片段内的LinearLayout,来自活动

时间:2014-11-25 23:22:17

标签: android android-layout android-fragments dynamic android-linearlayout

我已经在寻找解决方案,我找不到适合我情况的解决方案。我是Android开发的初学者,所以如果我做了明显错误的事情,请告诉我。

首先介绍我的应用程序结构。我为每个页面使用带有片段的导航抽屉,类似于Android文档中的导航抽屉示例。在其中一个页面上,我想在某些事件发生时动态地将TextViews添加到该页面的片段的LinearLayout。在这种情况下,事件并不重要 - 它只是在我的活动中调用一个方法。

所以在我的活动中我有一个方法 - addText()。每当调用addText()时,我想创建一个TextView,然后将其添加到片段中的LinearLayout。 (具有id diary_layout的LinearLayout)

我该怎么做?

我尝试过以下方法。调用此方法时,我看不到页面中的文本。

public void textAdd(){
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.fragment_diary, null);
    LinearLayout diaryLayout = (LinearLayout) contentView.findViewById(R.id.diary_layout);
    TextView newTextView = new TextView(this);
    newTextView.setText("Testing");
    diaryLayout.addView(newTextView);
}

这是片段的布局文件的结构:

<LinearLayout
android:id="@+id/theLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/diary_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MyActivity$PlaceholderFragment">
    </LinearLayout>
</ScrollView>

活动布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<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/transparent"
    android:dividerHeight="10dp"
    android:background="#FFFFFF"/>

提前致谢!

2 个答案:

答案 0 :(得分:0)

每次添加文字时,您绝对不想夸大内容。那会再次加载初始布局,这不是你想要的。

您没有展示如何构建片段,但根据您的描述,您希望在现有片段之一中将新TextView添加到LinearLayout。我希望你首先获取片段,然后调用片段中的一些方法来添加文本。类似的东西:

public void textAdd(){
    FragmentManager fm = getFragmentManager();
    MyFragment myFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
    if (myFragment != null) {
        LinearLayout diaryLayout = (LinearLayout) myFragment.findViewById(R.id.diary_layout);
        TextView newTextView = new TextView(this);
        newTextView.setText("Testing");
        diaryLayout.addView(newTextView);
    }
}

答案 1 :(得分:0)

我认为您需要在textview中添加布局参数..尝试这种方式..

  TextView newTextView = new TextView(this);
    newTextView.setText("Testing");
    newTextView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    diaryLayout.addView(newTextView);