Programmatically adding a layout + children

社会主义新天地 提交于 2021-01-28 06:31:58

问题


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.

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.

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.

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

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


回答1:


You are close you just forgot something

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

Example :

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

For finish you can add your inflate view to your container with

 linearLayout.addView(view);


来源:https://stackoverflow.com/questions/55677068/programmatically-adding-a-layout-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!