android - 纯文本视图未显示在 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>
解决方案
我建议您尝试使用约束布局而不是相对布局。如果您是新手,相对布局有时会很棘手。
在约束布局中,您创建一个视图,然后将其约束附加到某些视图的边缘,就是这样。不需要所有的 XML 代码tools:layout_editor_absoluteX
在这里,查看此视频:
推荐阅读
- python - 无法使用 python 访问 selenium 中的选择下拉列表中的选项-“元素当前不可见”
- shopify - 将侧边栏添加到 Debut Theme Shopify
- azure - Azure 身份验证重定向到不需要的 URL
- javascript - 组合二维数组然后按索引 0 排序的最快方法
- python - pyspark — 拆分后对 Array(StringType()) 类型列中的值求和的最佳方法
- c - 在十六进制编辑器上查找调试符号指向的变量的值
- android - 撰写 BasicTextField 焦点问题
- c# - 难以理解实现算法的逻辑 - C#
- bash - 学习 grep 命令并遇到了这个。它如何与尾巴结合使用?
- javascript - 如何使用 Node JS sdk 连接到 Web 套接字并在前端显示