【问题标题】:Can't set TextView to VISIBLE无法将 TextView 设置为可见
【发布时间】:2016-11-01 03:20:40
【问题描述】:

我在 onCreate 方法中将以下内容设置为可见。

findViewById(R.id.noDesignsCallToAction).setVisibility(View.VISIBLE);

在 XML 中,视图设置为可见性消失。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="vertical"
    android:layout_below="@id/toolbar"
    android:id="@+id/linearLayout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/go_ahead_create_something_n_it_s_really_easy_n_just_click_here_to_get_started"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/noDesignsCallToAction"
            android:padding="10dp"
            android:textSize="14sp"
            android:typeface="sans"
            android:visibility="gone"
            android:onClick="LaunchCustomizer"
            android:gravity="center_vertical|center_horizontal" />

</LinearLayout>

为什么 textview 在我的应用中不可见?

【问题讨论】:

  • 你能发布整个布局 XML 吗?
  • 如果你给它充气然后设置它的可见性,它会起作用吗?像 TextView tv = (TextView) findView...; tv.setVisibility(View.VISIBLE); 也许你的字符串 res 是空的?
  • 真正想要做什么?
  • 从未发现 setVisibility 有任何问题。发布您的整个 XML 和相关的 java 代码。可能由于某种原因,视图位于窗口之外,或者您在错误的时间调用了 setVisibility ......类似的事情

标签: java android textview imageview visibility


【解决方案1】:

确保首先设置内容视图。否则,由于在活动布局中找不到 ID,您将获得 NPE。

使用实例(不要调用findViewById(id).setVisibility(),因为与将 TextView 缓存为字段相比,它使用更多内存),然后设置可见性。

setContentView(R.layout.layout);
TextView temp = (TextView) findViewById(R.id.noDesignsCallToAction);
temp.setVisibility(View.VISIBLE);

【讨论】:

    【解决方案2】:

    给它充气,然后设置它的可见性。

        setContentView(R.layout.something);
    
    TextView tv = (TextView)   findView...;     
    
    tv.setVisibility(View.VISIBLE);
    

    也许字符串 res 是空的?

    【讨论】:

      【解决方案3】:

      在布局文件中更改 textview 的 textColor 或检查是否有任何视图没有与您的 textview 重叠。

      【讨论】:

      • 我有另一个 textView 与我感兴趣的 textView 重叠。我不能确切地说我是如何修复它的,因为它需要大量的试验和错误。只是那些挑剔的布局 xml 问题之一。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      相关资源
      最近更新 更多