简体   繁体   中英

Append a LinearLayout inside another LinearLayout programmatically

I am trying to create an activity that would add a custom view inside a LinearLayout. I am trying to add the LinearLayout inside the sample_answer_view.xml Here's the code of the sample_answer_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/answer_num_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="X)"
    android:layout_margin="10dp"/>

<Button
    android:id="@+id/answer_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Button" />

And here's the code for the SampleAnswerView.java

package com.example.sxoli_odigisis;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SampleAnswerView extends LinearLayout {
    private TextView answer_num_tv;
    private Button answer_btn;

    public SampleAnswerView(Context context){
        super(context);
        initializeViews(context);
    }

    public SampleAnswerView(Context context, AttributeSet attrs){
        super(context, attrs);
        initializeViews(context);
    }

    public SampleAnswerView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        initializeViews(context);
    }

    public void initializeViews(Context context){
        LayoutInflater inflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.sample_answer_view, this);
    }

}

How can I add this view for example 2 times inside a Linear Layout with code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

Using java that is added inside the MainActivity.java

So I am trying to create an app that gets possible answers from a database but the number of possible answers is not known before getting the items from the database. So let's say it says that question has 3 possible answers. Using a for loop it would generate 3 items. Those items would be a button and a textview that have been added to a LinearLayout(it is the code inside sample_answer_view.xml

The items should be added inside the MainActivity inside another LinearLayout.

Thank you in advance!

Try adding following code in your MainActivity code and accordingly adjust

LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourmainlayout);
 LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View itemBox = inflater.inflate(R.layout.the_layout_you_want_to_add);
 mainLayout.addView(itemBox);

if you want to change the button text of layout (main or itembox )

Button btn1=(TextView)itemBox.findviewbyId(R.id.button1);
btn1.settext("TEST");

Here is what you need

In layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

In the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blank);

    LinearLayout root = findViewById(R.id.root);
    int numberOfAnswers = 3; //assume 3
    for (int i = 0; i < numberOfAnswers; i++) {
        View child = View.inflate(this, R.layout.item, null);
        root.addView(child);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM