以编程方式添加布局+子项

问题描述 投票:0回答:1

我目前正在学习Android编程的基础知识。我可以用一个页面和几个按钮创建一个静态应用程序。下一步是学习动态创建应用程序,就像我在Web应用程序中习惯的那样。

我要做的是创建一个简单的类似Facebook的应用程序,其中包含一些时间轴对象,一些文本,一个像按钮和它的日期。有一个简单的NodeJS服务器响应带有JSON文件的http GET请求。解析哪个,并且对于数组中的每个对象,应该创建一个时间轴对象,就像在facebook上一样。

在我的主页上,我正在创建一些动态内容来包含时间轴对象。

    ConstraintLayout container = activity.findViewById(R.id.container);
    container.removeAllViews();
    TextView nav_txt = activity.findViewById(R.id.nav_txt);
    nav_txt.setText("Home");
    swipeRefreshLayout = new SwipeRefreshLayout(activity);
    ScrollView scrollView = new ScrollView(activity);
    container.addView(swipeRefreshLayout);
    scrollView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ConstraintLayout.LayoutParams.MATCH_PARENT));
    linearLayout = new LinearLayout(activity);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setId(R.id.linearLayout);
    swipeRefreshLayout.addView(scrollView);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshData();
        }
    });
    scrollView.addView(linearLayout);

这很容易,视图不需要任何约束,因为一切都与父级相同。

时间轴对象更复杂,有很多约束和不同大小的子项,所以我想,我创建了一个自定义XML文件,时间轴对象已经定义,抓住它并修改它以满足我的需要。

我的timeline_message.xml看起来像这样:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:id="@+id/timeline_msg"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginTop="8dp"
    android:background="@color/backgroundColor"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/like_btn"
        android:layout_width="90dp"
        android:layout_height="91dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/colorPrimary"
        android:text="Like"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/username"
        android:layout_width="272dp"
        android:layout_height="55dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:text="text"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/like_btn"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/date"
        android:layout_width="91dp"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.003"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

而我正试图抓住这个对象+这样的孩子:

    LinearLayout linearLayout = activity.findViewById(R.id.linearLayout);
    ConstraintLayout timelineMsg = activity.findViewById(R.id.timeline_msg);
    linearLayout.addView(timelineMsg);

但没有任何反应。这样的方法可能吗?或者您是否需要使用layoutparams和constraints e.t.c.手动编写所有代码?

android android-layout android-linearlayout android-design-library
1个回答
0
投票

你很近,你忘了什么

如果要使用timeline_message.xml布局,则需要先对其进行充气

示例:

  LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

然后你可以使用你的inflater给视图充气

  View view = inflater.inflate(R.layout.timeline_message, null);

您现在可以使用视图查找元素

 ConstraintLayout timelineMsg = view.findViewById(R.id.timeline_msg);

完成后,您可以将充气视图添加到容器中

 linearLayout.addView(view);
© www.soinside.com 2019 - 2024. All rights reserved.