在Fragment中的线性布局中动态添加视图

时间:2016-07-21 18:56:46

标签: android xml android-layout android-fragments dynamically-generated

我正在尝试将FragView添加到fragmentin onCreateView中的嵌套linearLayout,我不知道这是否是正确的方法,对于android来说是新的! 提前致谢

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
    List<LifeBerrysXmlParser.Item> items = (ArrayList<LifeBerrysXmlParser.Item>) getArguments().getSerializable("List");
    TextView mainHeading = (TextView) rootView.findViewById(R.id.mainHeading);
    ImageView mainImage = (ImageView) rootView.findViewById(R.id.articleMainImage);
    LinearLayout articleLayout = (LinearLayout) rootView.findViewById(R.id.articleLayout);
    TextView tev = new TextView(getActivity());
    tev.setText("Hello..............");
    articleLayout.addView(tev);
    LifeBerrysXmlParser.Item item = items.get(getArguments().getInt("position"));
    String articleMainHeading = item.mainHeading;
    String articlemainImage = item.mainImage;
    mainHeading.setText(articleMainHeading);
    if (!articlemainImage.isEmpty()) {
        Picasso.with(getContext()).load(articlemainImage).into(mainImage);
    }
    return rootView;
}
谁能帮助我,我做错了什么?

这是我的片段

的xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:id="@+id/content"
        android:padding="1dp"
        android:layout_weight="1"
        android:background="#000000"
        android:layout_gravity="center">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ffffff"
            android:scrollIndicators="right">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/articleMainImage"
                    android:maxHeight="300dp"/>
                <TextView
                    style="?android:textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="?android:attr/listPreferredItemHeightLarge"
                    android:layout_weight="1"
                    android:lineSpacingMultiplier="1.2"
                    android:id="@+id/mainHeading"
                    android:textColor="#ff6435"
                    android:clickable="false"
                    android:layout_gravity="center"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/articleLayout"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Read More"
                    android:textSize="15dp"
                    android:textStyle="bold"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:focusable="false"
                    android:gravity="center"
                    android:onClick="readMore"
                    android:padding="1dp"
                    android:textColor="@android:color/holo_green_light" />
            </LinearLayout>
        </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

您的文章布局有误。您的文章布局具有水平方向,并且它的子TextView具有match_parent宽度,因此当您添加新子项时,它将位于屏幕之外。将方向更改为垂直方向:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/articleLayout"
            android:orientation="vertical">      <---- Change this line
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Read More"
                android:textSize="15dp"
                android:textStyle="bold"
                android:layout_gravity="center"
                android:clickable="true"
                android:focusable="false"
                android:gravity="center"
                android:onClick="readMore"
                android:padding="1dp"
                android:textColor="@android:color/holo_green_light" />
        </LinearLayout>