繁体   English   中英

尝试将新的TextView添加到屏幕底部并将其堆叠

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

因此,当前我有一个EditText字段和一个按钮,我希望每次按下该按钮时,都可以在屏幕底部添加名称,但要继续将其添加到屏幕底部,而不是一行。 它目前仅在一行上打印,我不知道如何在要添加的文本上放大字体。 很抱歉这个愚蠢的问题。 这是我在主文件和XML bc中拥有的内容,它不允许我在其下面发布

    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>

您的LinearLayout方向似乎是horizontal 它应该是vertical ,您希望在下面添加这些行。

话虽这么说,考虑为该任务使用RecyclerView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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