如何将字符串添加到 TextView / ScrollView?

How to add Strings to a TextView / ScrollView?

我是 android 开发的新手 抱歉!

我想通过一个按钮将医生值作为字符串(名字、姓氏和其他一些)作为一个条目(我试图用对象这样做)添加到我的滚动视图中。我知道我需要一个布局和一个文本视图才能这样做,但我的虚拟机崩溃了。 据我所知,我必须将我的文本视图(带有字符串)放在布局中,并将布局放在滚动视图中(我错了吗?)。

我在不同的网站上尝试了几种方法,它们都提供了类似的解决方案,但到目前为止都没有效果。我将 Android 6.0 与 android studio 一起使用,并使用设计视图设计了我的 UI 并调整了 XML 文件中的一些代码。

public void addDoctor(View view) {

    setContentView(R.layout.activity_main);
    // standard java class Doctor
    Doctor doc = new Doctor("Foo", "Boo");

    // this is the ScrollView I generated with the design
    ScrollView sv = (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
    TextView tv = new TextView(this);
    // trying to fix the parent problem here
    if(tv.getParent() != null) {
        ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
    }
    //this should be "Foo"
    tv.setText(doc.getDocName());

    // this is the layout I generated with the design "linlay"
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);
    sv.addView(ll);

    //only one child object! -> in the error log
    ll.addView(tv);
    setContentView(view);
}

我希望对象的字符串出现在滚动视图中,但错误日志显示 "ScrollView can host only one direct child" 我试图用 if 语句修复它,但它似乎不会影响我的代码。

你能帮我解决这个问题吗?我错过了什么吗?

谢谢!

去掉最后的setContentView(view);,你已经在第一行设置了这个布局

setContentView 在大多数情况下应该在 onCreate 方法中调用一次。如果你多次调用 setContentView 你基本上是覆盖了前一个

还有这个

if(tv.getParent() != null) {
    ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}

是不必要的,您正在上面创建新的 TextView 行,它不会有 shure

的父级

你为什么不使用 RecyclerView?然后你不需要 ScrollView。并且不要调用 setContentView(view);倍数。你只需要做1次。

您需要创建多行 TextView

<ScrollView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:layout_marginLeft="15dp"
       android:layout_marginRight="15dp"
       android:layout_marginTop="20dp"
       android:fillViewport="true">

       <TextView
           android:id="@+id/txtquestion"
           android:layout_width="fill_parent"
           android:layout_height="match_parent"
           android:background="@drawable/abs__dialog_full_holo_light"
           android:lines="20"
           android:scrollHorizontally="false"
           android:scrollbars="vertical"
           android:textSize="15sp" />

   </ScrollView>

As far as I understand I have to put my Text View (with the Strings) in the Layout and the Layout in the Scroll View (am I wrong?).

你说得对。

<ScrollView>
   <LinearLayout>
     <TextView></TextView>
   </LinearLayout>
</ScrollView>

如何正确添加医生

error log says that "ScrollView can host only one direct child"

您收到错误是因为您已经在 <ScrollView> 中添加了线性布局,所以您不必调用 sv.addView(ll); 因为它已经在里面(而且您不能添加多个标准滚动视图中的布局)

所以你引用的 ScrollView 没用。

如果你 xml 是这样的:

<ScrollView>
    <LinearLayout>

    </LinearLayout>
</ScrollView>

你可以这样实现你的结果:

public class MyActivityDoctors extends Activity {

    ScrollView sv;


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

        sv =  (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
    }

    public void addDoctor(Doctor doctor)
    {
        //Linear Layout inside your ScrollView
        LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);

        //Create a new TextView with doctor data
        TextView tv = new TextView(this);
        tv.setText(doctor.getDocName());

        //Adding textView to LinearLayout
        ll.addView(tv);
    }

}