【问题标题】:Text not visible when used setAlpha()使用 setAlpha() 时文本不可见
【发布时间】:2017-09-05 14:31:09
【问题描述】:

我只是想进行夜间模式活动,但是当背景变黑时,屏幕上看不到文本。切换到深色模式时文本消失。有什么解决办法吗?

我试过tv.setTextColor(Color.WHITE);在黑屏时也不起作用。

任何帮助将不胜感激。提前致谢。

图片如下:

Image 1

Image 2

这是 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="match_parent"
    tools:context="com.saipriyank.daynight.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:layout_margin="50px"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/background_view"
        android:background="@android:color/background_dark"
        android:alpha="0"/>



    <com.mahfa.dnswitch.DayNightSwitch
        android:id="@+id/dayNight"
        android:layout_width="80dp"
        android:layout_height="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这里是 Java 代码:

package com.saipriyank.daynight;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mahfa.dnswitch.DayNightSwitch;
import com.mahfa.dnswitch.DayNightSwitchListener;

public class MainActivity extends AppCompatActivity {

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

        DayNightSwitch dayNight = (DayNightSwitch)findViewById(R.id.dayNight);
        final View background_view = (View)findViewById(R.id.background_view);
        final TextView tv = (TextView)findViewById(R.id.tv);

        dayNight.setDuration(450);
        dayNight.setListener(new DayNightSwitchListener() {
            @Override
            public void onSwitch(boolean isNight) {
                if(isNight){
                    Toast.makeText(MainActivity.this, "Night Mode Enabled",Toast.LENGTH_SHORT).show();
                    tv.setAlpha(0f);
                    background_view.setAlpha(1f);
                }

                else {
                    background_view.setAlpha(0f);
                }

            }
        });
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您正在通过调用 tv.setAlpha(0f) 使 TextView 不可见。

    如果要设置文字颜色为白色,可以使用tv.setTextColor(Color.White);

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多