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

时间:2019-04-14 15:38:23

标签: android android-layout android-linearlayout android-design-library

我目前正在学习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文件,通过ID进行抓取并对其进行修改以满足我的需要

我的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和约束的代码?

1 个答案:

答案 0 :(得分:0)

靠近你,你忘了一些东西

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

示例:

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

然后,您可以使用充气机为视图充气

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

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

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

最后,您可以使用

将膨胀视图添加到容器中
 linearLayout.addView(view);
相关问题