简体   繁体   中英

Trying to add new TextView to Bottom of Screen and have them Stacked

So currently I have an EditText field and a button and I want every time when that button is pushed to add the name at the bottom of the screen, but continuing to add them to the bottom of the screen rather than one line. It's currently only printing on one line and I don't know how to make the font bigger on the text that is being added. Sorry for the dumb question. This is what I have in the main file and the XML bc it wouldn't let me post right below it

    package com.example.bryce.
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.util.Log;
    import android.view.View;
    import android.content.
    public class MainActivity extends 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LinearLayout mLayout=findViewById(R.id.linearLayout);
        final EditText mEditText=findViewById(R.id.StudentNameEdt);
        final Button mButton=findViewById(R.id.insertButton);
        TextView textView=new TextView(this);
        textView.setText("New TExt");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayout.addView(createNewTextView(mEditText.getText().toString()));
            }
        });



    }
    private TextView createNewTextView(String text)
    {
        final LinearLayout mLayout=findViewById(R.id.linearLayout);
        String newLine=System.getProperty("line.separator");
        final LinearLayout.LayoutParams lparams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) mLayout.getLayoutParams();
        final TextView textView=new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New texT:: "+text+newLine);

        return textView;
    }
    }
And here is the XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 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="900dp"
        tools:context=".MainActivity">


        <TextView
            android:id="@+id/NewClassTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Welcome!"
            android:textSize="30sp"
            android:textStyle="bold"
            android:typeface="serif"
            app:fontFamily="sans-serif-smallcaps" />

        <TextView
            android:id="@+id/WelcomeAgain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/NewClassTitle"
            android:text="Fill out the Form Below to Register"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif"
            app:fontFamily="sans-serif-smallcaps" />

        <EditText
            android:id="@+id/ClassNumEdt"
            android:layout_width="324dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="222dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Class 
        <Button
            android:id="@+id/insertButton"
            android:layout_width="wrap_content"
            android:layout_height="38dp"
            android:layout_above="@+id/StudentView"
            android:layout_toStartOf="@+id/NewClassTitle"
            android:text="Insert"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/StudentNameEdt"
            android:layout_width="337dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="295dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Student Name" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="0dp"
            android:orientation="horizontal"></LinearLayout>


    </RelativeLayout>
</ScrollView>

Your LinearLayout orientation appears to be horizontal . It should be vertical where you to expect the lines to be added below.

That being said, consider using a RecyclerView for the task.

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