首页 > 解决方案 > 纯文本视图未显示在 android 模拟器的应用程序中,但显示在 Android Studio 设计选项卡中

问题描述

我是 android 的初学者,我只是在学习一个教程。问题是纯文本视图没有显示在 android 模拟器的应用程序中,但它显示在 android studio 设计选项卡中。我不明白为什么它没有显示。相对视图有什么问题吗?有人可以帮帮我吗。提前致谢 。
我的 mainActivity 代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button2.setOnClickListener {
            Toast.makeText(this, "hello world", Toast.LENGTH_SHORT).show()
        }
        button3.setOnClickListener {
            try {
                val textValue : String = editTextTextPersonName?.text.toString()
                Toast.makeText(this, textValue, Toast.LENGTH_SHORT).show()
            } catch (e : Exception){
                Log.i("myactivity", "$e")
            }
        }

    }
}

和我的 xml 文件代码

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".MainActivity"
    android:padding="5dp">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextTextPersonName"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:layout_marginEnd="155dp"
        android:layout_marginBottom="491dp"
        android:text="@string/button"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="126dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="156dp"
        android:layout_marginBottom="273dp"
        android:text="@string/pavan" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/hint"
        android:inputType="textPersonName"
        android:text="@string/punna" />


</RelativeLayout>

图片 带有设计选项卡和应用程序视图的图像

标签: androidandroid-studioandroid-layoutkotlin

解决方案


我建议您尝试使用约束布局而不是相对布局。如果您是新手,相对布局有时会很棘手。

在约束布局中,您创建一个视图,然后将其约束附加到某些视图的边缘,就是这样。不需要所有的 XML 代码tools:layout_editor_absoluteX

在这里,查看此视频:

https://www.youtube.com/watch?v=mtYBBInIIE8


推荐阅读