fiv*_*cer 3 android android-layout android-studio
我刚刚安装了 Android Studio 并在其中创建了一个项目,但是每当我在其中添加 TextView 时,它都没有显示任何内容,即使TextViewAndroid Studio 已经创建的Activity.xml代码也没有显示
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="67dp"
tools:text="Hello World" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
tools:text="sdfsfsfsfs" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Hello World" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。
小智 8
可能是一个迟到的答案,但只要它对某人有帮助。
您正在使用最后一个文本视图的属性tools:text。tools 命名空间仅用于 android studio 中的布局预览。
为了使 textview 在运行时显示某些内容,您必须使用 android 命名空间和android:text属性,如下所示:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
Run Code Online (Sandbox Code Playgroud)
此外,只有第一个文本视图受到约束。尝试向其余电视添加约束。
而且,如果您的设计窗口没有显示任何文本,而是显示空白屏幕,您可能需要更改设计窗口中的预览主题以匹配您的应用程序主题(例如:Material 主题、Light、AppTheme 等) 。按钮,现在显示“Material Theme”
试试你的约束布局。
<android.support.constraint.ConstraintLayout 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="wrap_content"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Hello World!"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:text="sdfsfsfsfs" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Hello World"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
此外,id/textview在应用程序运行时不会呈现任何内容,因为没有android:text属性。tools:text仅适用于布局编辑器。
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Hello World" />
Run Code Online (Sandbox Code Playgroud)
如果你要使用约束布局,你需要约束你的小部件。首先尝试使用线性布局,如果您只想显示带有 hello world 的文本视图,它会更简单
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#134A80">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold"
android:textColor="#FFF"
android:layout_marginTop="8dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6675 次 |
| 最近记录: |