2017-09-05 63 views
0

我只是试图进行夜间模式活动,但当背景变黑时,文本在屏幕上不可见。文字在切换到黑暗模式时消失。任何解决方案使用setAlpha()时,文本不可见()

我试过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); 
       } 

      } 
     }); 
    } 
} 

回答

0

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

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