Add multiple custom views to layout programmatically

后端 未结 3 1971
無奈伤痛
無奈伤痛 2020-12-24 06:30

If I for example have empty layout like this:

layout1.xml



        
相关标签:
3条回答
  • 2020-12-24 06:43

    Try this method

    Add id to LinearLayout 'layout1.xml':

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/firstlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    
    </LinearLayout>
    

    In onCreate:

    LinearLayout firstlayout = (LinearLayout) findViewById(R.id.firstlayout);
    
    LinearLayout secondlayoout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.layout2, null); // inflating view from xml
    TextView btn1 = (TextView) secondlayoout.findViewById(R.id.button1);
    btn1.setText("TEST");
    
    firstlayout.addView(secondlayoout);
    
    0 讨论(0)
  • 2020-12-24 06:47

    layout1.xml contains ScrollView as parent layout and main LinearLayout as child if no row item more screen size ScrollView handle overflow item with scroll:

    layout1.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/my_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
        </LinearLayout>
    </ScrollView>
    

    Use LayoutInflater to add row item to parent LinearLayout:

    private LinearLayout my_linear_layout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        my_linear_layout = (LinearLayout) findViewById(R.id.my_linear_layout);
    
        for (int i = 1; i <= 5; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout2, null);
            TextView button1 = (TextView) view.findViewById(R.id.button1);
            Button button2 = (Button) view.findViewById(R.id.button2);
            TextView button3 = (TextView) view.findViewById(R.id.button3);
    
            button1.setText("HELLO " + i);
            button2.setText("HELLO " + i);
            button3.setText("HELLO " + i);
            my_linear_layout.addView(view);
        }
    }
    
    0 讨论(0)
  • 2020-12-24 07:00

    You can inflate the layout2.xml file, edit the texts, and add it to the first layout:

    public class MyActivity extends Activity {
    
        private ViewGroup mLinearLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout1);
            mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
            addLayout("This is text 1", "This is first button", "This is second Button");
        }
    
        private void addLayout(String textViewText, String buttonText1, String buttonText2) {
            View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);
    
            TextView textView = (TextView) layout2.findViewById(R.id.button1);
            Button button1 = (Button) layout2.findViewById(R.id.button2);
            Button button2 = (Button) layout2.findViewById(R.id.button3);
    
            textView1.setText(textViewText);
            button1.setText(buttonText1);
            button2.setText(buttonText2);
    
            mLinearLayout.addView(layout2);
        }
    }
    

    You may want to change android:layout_height of the layout2.xml root view to wrap_content.

    0 讨论(0)
提交回复
热议问题