以编程方式添加布局+子项 [英] Programmatically adding a layout + children

查看:105
本文介绍了以编程方式添加布局+子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently learning the basics of Android programming. I'm able to create a static app with one page and a few buttons. The next step is learn to dynamically create apps, like i'm used to in webapplications.

我想做的是创建一个简单的类似Facebook的应用程序,其中包含一些时间轴对象,一些文本,一个类似按钮以及它的放置日期.有一个简单的NodeJS服务器,它使用JSON文件响应http GET请求.解析后,对于数组中的每个对象,都应该创建一个时间轴对象,就像在Facebook上一样.

What i'm trying to do is create a simple facebook-like app with some timeline objects, with some text, a like button and the date it was placed. There is a simple NodeJS server which responds to a http GET request with a JSON file. Which is parsed and for each object in the array there should be a timeline object created, just like there is on facebook.

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

On my homepage i'm creating some dynamic content to contain the timeline objects.

    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);

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

This is easy, the views don't need any constraints, because everything is the same height as the parent.

时间轴对象更加复杂,有很多约束和不同大小的子对象,所以我想,我用已经定义的时间轴对象创建了一个自定义XML文件,通过ID进行抓取并对其进行修改以满足我的需要

The timeline objects are more complicated, with a lot of constraints and children with different sizes, so i thought, i create a custom XML file with the timeline object already defined, grab it by the ID and modify it to suit my needs.

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

My timeline_message.xml looks like this:

<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>

我正试图像这样抓住物体和孩子:

And i'm trying to grab the object + children like this:

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

但是什么也没发生.这样的方法可行吗?还是您需要手动编写所有带有layoutparams和约束的代码?

But nothing happens. Is an approach like this possible? Or do you need to write all that code manually with layoutparams and constraints e.t.c.?

推荐答案

您很亲密,只是忘记了一些东西

You are close you just forgot something

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

If you want to use your timeline_message.xml layout you need to inflate it before

示例:

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

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

Then you can use your inflater for inflate a view

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

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

You can now use your view for find your element

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

最后,您可以使用

 linearLayout.addView(view);

这篇关于以编程方式添加布局+子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆