【问题标题】:TextViews invisible inside LinearLayout?文本视图在 LinearLayout 中不可见?
【发布时间】:2013-06-06 06:20:02
【问题描述】:

我在LinearLayout 中有一些TextViews。在运行时,LinearLayout 是可见的,但 TextViews 都不可见。这是 XML:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5384615384615385"
        android:scaleType="fitXY"
        android:adjustViewBounds="true">
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    >
    <TextView
            android:id="@+id/onezero"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="one"
            android:typeface="sans"
            android:layout_weight="1"
            android:layout_margin="17dp"
            android:textIsSelectable="true"
            android:clickable="true" />
    <TextView
            android:id="@+id/oneone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="two"
            android:typeface="sans"
            android:layout_margin="17dp"
            android:textIsSelectable="true"
            android:clickable="true" />
    <TextView
            android:id="@+id/onefour"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="three"
            android:layout_weight="1"
            android:typeface="sans"
            android:layout_margin="17dp"
            android:textIsSelectable="true"
            android:clickable="true" />
    <TextView
            android:id="@+id/other"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="four"
            android:layout_margin="17dp"
            android:typeface="sans"
            android:layout_weight="1" 
            android:textIsSelectable="true"
            android:clickable="true" />
    </LinearLayout>
    <ScrollView 
        android:id="@+id/scrollview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">
    <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"

            android:typeface="sans"
            android:textSize="17sp" 
            />

    </ScrollView>
    </LinearLayout>
</FrameLayout>

如何让它们可见?

编辑:访问此布局的代码:

void fragment(){
                ten.setTextColor(0xFFFFFF);//These are the 4 TextViews
        eleven.setTextColor(0xFFFFFF);
        fourteen.setTextColor(0xFFFFFF);
        other.setTextColor(0xFFFFFF);

            ten.setVisibility(View.VISIBLE);
            eleven.setVisibility(View.VISIBLE);
        fourteen.setVisibility(View.VISIBLE);
        other.setVisibility(View.VISIBLE);
}

我初始化文本视图的代码:

ten=(TextView)findViewById(R.id.onezero);
eleven=(TextView)findViewById(R.id.oneone); 
fourteen=(TextView)findViewById(R.id.onefour);
other=(TextView)findViewById(R.id.other);

编辑:我尝试点击 LinearLayout 区域并且 TextViews 正在注册点击。这意味着它们在那里但不可见...

【问题讨论】:

  • 我运行了你的代码..它工作正常...你能把访问这个视图的代码展示给活动吗?
  • +1 @amal,视图可见
  • @amal 添加了代码。
  • @vergilcorleone 将框架布局的背景颜色设置为红色,然后查看框架布局本身是否显示出来。
  • @Su-AuHwang 嗨,我将TextViews 的背景设置为红色,现在它们显示为红色矩形,但其中看不到任何文本,此外,它们正在注册点击,即表示文本存在,但不知何故不可见。

标签: android xml android-listview android-linearlayout


【解决方案1】:

你申请的颜色是白色的……

 0xFFFFFF

尝试更改颜色以与您的布局颜色形成对比。

编辑:

我花了很长时间才意识到这一点。用下面的代码试试吧:

 textview.setTextColor(Color.parseColor("#bdbdbd"));

【讨论】:

  • 我的布局颜色是黑色,所以白色应该是可见的。
  • 好的..你能告诉我们更多的代码吗..你使用 findviewbyid 调用非 java 资源的代码 sn-p。
  • 我尝试点击 LinearLayout 区域并且 TextViews 正在注册点击...这意味着它们在那里但不可见...
  • 我将文本视图的背景颜色更改为红色,现在红色矩形可见,但其中没有可见文本..
  • 我在下面添加了答案。使用 Color.WHITE 而不是十六进制有效。
【解决方案2】:

您正在使用 android:layout_weight - 基于比率的空间分布。要使其工作,您必须设置:

android:layout_width="0dp"

【讨论】:

  • 我这样做了,但它们仍然不可见。
【解决方案3】:

在你的xml中添加这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

然后在java类中改变你的文本颜色

使用

000000

改为

0xFFFFFF

【讨论】:

    【解决方案4】:

    用这个前缀改变你的布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    

    在 FrameLayout android:layout_height="0dp" 这将使视图不可见,可能用于 layout_width 的应该是 wrap_content

    【讨论】:

    • 我发布的这个布局只是大布局的一小部分。
    • 如果是这样?然后发布所有有助于解决您的问题的代码。
    • 从你的代码开始,我已经更新了答案看看。仍然没有解决你必须显示完整的xml代码。
    • 不,我使用的是权重,所以 framelayout 的宽度应该是 0dp。
    • 那么您还必须显示剩余的 xml 代码。我运行你的代码,它正确地显示了所有的文本视图。
    【解决方案5】:
    You have not assigned the layout_height of your parent layout i.e. FrameLayout. Try this:
    
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5384615384615385"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal" >
    
                <TextView
                    android:id="@+id/onezero"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="17dp"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:text="one"
                    android:textIsSelectable="true"
                    android:typeface="sans" />
    
                <TextView
                    android:id="@+id/oneone"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="17dp"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:text="two"
                    android:textIsSelectable="true"
                    android:typeface="sans" />
    
                <TextView
                    android:id="@+id/onefour"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="17dp"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:text="three"
                    android:textIsSelectable="true"
                    android:typeface="sans" />
    
                <TextView
                    android:id="@+id/other"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="17dp"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:text="four"
                    android:textIsSelectable="true"
                    android:typeface="sans" />
            </LinearLayout>
    
            <ScrollView
                android:id="@+id/scrollview1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="vertical" >
    
                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="17sp"
                    android:typeface="sans" />
            </ScrollView>
        </LinearLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案6】:

      显然,使用十六进制代码表示颜色:

          ten.setTextColor(0xFFFFFF);//These are the 4 TextViews
          eleven.setTextColor(0xFFFFFF);
          fourteen.setTextColor(0xFFFFFF);
          other.setTextColor(0xFFFFFF);
      

      不起作用。我把0xFFFFFF换成了Color.WHITE

          ten.setTextColor(Color.WHITE);//These are the 4 TextViews
          eleven.setTextColor(Color.WHITE);
          fourteen.setTextColor(Color.WHITE);
          other.setTextColor(Color.WHITE);
      

      这很好用。

      编辑:找出十六进制代码不起作用的原因。我们需要在每个十六进制的开头附加一个透明十六进制。例如,如果您希望颜色 80% 不透明,以下是如何找到透明度十六进制:

      255 * 0.8 = 204
      

      现在将 204 转换为十六进制(使用 google),您将得到 0xCC。在十六进制代码之前添加这个,你会得到0xCCFFFFFF。使用它可以完美地工作。

      【讨论】:

        猜你喜欢
        • 2012-11-09
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        相关资源
        最近更新 更多