【问题标题】:Issues setting my textView via MainActivity.java通过 MainActivity.java 设置我的 textView 的问题
【发布时间】:2019-09-15 06:16:08
【问题描述】:

我正在尝试在 MainActivity.java 中设置文本,但该文本未能出现在模拟器中。

我的activity_main.xml

> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="204dp"
        android:text="Show Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="2dp"
        android:layout_height="17dp"
        android:layout_marginTop="76dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的 MainActivity.java

> package com.example.shownamenow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Button myButton;
    private TextView showText;


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

        myButton = findViewById(R.id.button);
        showText = findViewById(R.id.textView);

        showText.setText("Hello I am here!");
    }
}

我在 MainActivity.java 中遇到错误,提示“setText 中的字符串文字无法翻译。使用 android 资源...”。所以我改用字符串资源:showText.setText(R.String.Hello) 并在 strings.xml 中创建了值为“Hello I am here”的资源。仍然当我在模拟器中加载应用程序时,文本拒绝出现。我做错了什么???

【问题讨论】:

  • 分享错误日志。
  • layout_width="wrap_content"constraintEnd_toEndOf="parent"constraintStart_toStartOf="parent" 一起使用是没有意义的,因为无论如何它都会拉伸它,所以layout_width="wrap_content" 不会产生任何影响。这里的问题是高度太小,如果您不提供这两个约束,只需使用layout_height="wrap_content"
  • 设置 textview 宽度为 0dp 或匹配父级

标签: java android textview settext


【解决方案1】:

这是因为文本视图的宽度非常小,请查看下面的代码

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="204dp"
        android:text="Show Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 因为您有 End_toEndOf 和 Start_toStartOf,所以最好将 android:layout_width="wrap_content" 替换为 android:layout_width="0dp" 以进行优化
【解决方案2】:

正如您在 xml 文件中看到的那样,

<TextView
    android:id="@+id/textView"
    android:layout_width="2dp"
    android:layout_height="17dp"
    android:layout_marginTop="76dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

您的宽度属性太小而无法看到。 尝试使用更大的值,例如

android:layout_width="50dp"

【讨论】:

    【解决方案3】:

    正如其他两个答案中所提到的 - 您的 android:layout_width="2dp" 属性使您的 textView 太小而无法被注意到。

    上述两个答案都适合您的情况,但我想补充一点

    我想在回答中添加的内容:

    • 即使您将布局宽度更改为 50dp,也不能保证您会在屏幕上看到您的视图。

    • 由于不同的手机在使用固定尺寸尺寸时屏幕尺寸不同,因此某些视图在一台设备上看起来不错,但在另一台设备上可能看起来不太好(如果您放置太多视图,您的视图可能无法在屏幕上看到大小固定)。


    如果您已经选择使用ConstraintLayout,您可以使用这两个属性来使您的视图响应屏幕尺寸:

    • app:layout_constraintWidth_percent

    • app:layout_constraintHeight_percent

    例如:

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent=".2"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    在上面的例子中,我告诉我的视图等于屏幕尺寸的宽度20%和高度10%

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多