2012-10-01 13 views
5

Defino explícitamente un color negro en el fondo de mi LinearLayout (usando @android: color/negro en mi XML), pero a veces (a menudo después de una rotación de pantalla), el negro es convirtiéndose en un color gris (o negro transparente).Android - fondo negro convertido en gris

Este problema aparece en muchos dispositivos: ambos en emulador, Acer Liquid (Android 2.2) y Galaxy Nexus (Android 4.1).

Imágenes: // Buggy viewNot buggy view

Aquí está mi XML y la actividad de código:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:minHeight="150dp"> 

      <ImageView 
       android:id="@+id/projectview_description_image" 
       android:layout_width="match_parent" 
       android:layout_height="150dp" 
       android:contentDescription="@string/projectview_description_image_description" 
       android:scaleType="centerCrop" 
       android:src="@drawable/project_nophoto" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="vertical" 
       android:id="@+id/projectview_description_overlay" 
       android:paddingTop="5dp" 
       android:paddingLeft="10dp" 
       android:paddingBottom="5dp" 
       android:background="@android:color/black" 
       android:height="35dp"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/projectview_description_title" 
        android:textAllCaps="true" 
        android:textColor="@android:color/white" 
        android:textSize="20dp" 
        android:textStyle="bold"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/projectview_description_baseline" 
        android:textColor="@android:color/white" 
        android:textStyle="italic" /> 

      </LinearLayout> 

     </RelativeLayout> 

     <!-- This LinearLayout background is buggy --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:orientation="horizontal" 
      android:background="@android:color/black"> 

      <ImageView 
       android:layout_width="175dp" 
       android:layout_height="25dp" 
       android:layout_gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/project_status_finish" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:gravity="right" 
       android:paddingRight="10dp" 
       android:text="@string/projectview_description_website" 
       android:textColor="@color/website_link" 
       android:textSize="15dp" 
       android:id="@+id/projectview_description_website" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/projectview_description_about_container" 
      android:padding="10dp" 
      android:paddingBottom="0dp"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/projectview_description_about" 
       android:textAllCaps="true" 
       android:textStyle="bold" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/projectview_description_about_text" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/projectview_description_questions_container" /> 

    </LinearLayout> 

</ScrollView> 

Actividad:

public class ProjectViewActivity extends Activity implements OnClickListener { 
    private boolean displayMenu = false; 
    private Intent shareIntent; 
    private FavoriteSqlite db; 
    private I4pProjectTranslation project; 

    @TargetApi(14) 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     if(displayMenu) { 
      // Inflate menu only if it hasn't been done before 
      if(menu.size() == 0) { 
       // Inflating the menu 
       MenuInflater inflater = getMenuInflater(); 
       inflater.inflate(R.menu.projectview, menu); 

       // Creating share intent 
       Intent prepareShareIntent = new Intent(Intent.ACTION_SEND); 
       prepareShareIntent.putExtra(Intent.EXTRA_TEXT, UriHelper.getProjectUrl(project)); 
       prepareShareIntent.putExtra(Intent.EXTRA_SUBJECT, project.getTitle()); 
       prepareShareIntent.setType("text/plain"); 
       shareIntent = Intent.createChooser(prepareShareIntent, getResources().getText(R.string.projectview_menu_share_dialog)); 
      } 

      // Defining favorite state 
      MenuItem favoriteItem = menu.getItem(0); 
      if(db.isFavorite(project)) 
       favoriteItem.setTitle(R.string.projectview_menu_favorites_remove); 
      else 
       favoriteItem.setTitle(R.string.projectview_menu_favorites_add); 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
     case android.R.id.home: 
      if(getIntent().getData() != null) { 
       Intent intent = new Intent(this, HomepageActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
      } else 
       finish(); 
      break; 
     case R.id.projectview_favorite: 
      Toast t; 
      if(db.isFavorite(project)) { 
       db.removeFavorite(project); 
       t = Toast.makeText(this, getResources().getString(R.string.projectview_toast_favorites_remove, project.getTitle()), Toast.LENGTH_SHORT); 
      } else { 
       db.addFavorite(project); 
       t = Toast.makeText(this, getResources().getString(R.string.projectview_toast_favorites_add, project.getTitle()), Toast.LENGTH_SHORT); 
      } 
      t.show(); 
      break; 
     case R.id.projectview_share: 
      startActivity(shareIntent); 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @TargetApi(11) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if(Build.VERSION.SDK_INT < 11) 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.loading); 
     db = new FavoriteSqlite(this); 

     if(Build.VERSION.SDK_INT >= 11) 
      getActionBar().setDisplayHomeAsUpEnabled(true); 

     project = (I4pProjectTranslation) getLastNonConfigurationInstance(); 
     if(project != null) 
      displayProject(); 
     else { 
      String projectLang; 
      String projectSlug; 

      Uri data = getIntent().getData(); 
      if(data != null) { 
       List<String> path = data.getPathSegments(); 
       projectLang = path.get(0); 
       projectSlug = path.get(2); 
      } else { 
       Bundle extras = getIntent().getExtras(); 

       if(extras.containsKey("project_title")) 
        setTitle(extras.getString("project_title")); 

       projectLang = extras.getString("project_lang"); 
       projectSlug = extras.getString("project_slug"); 
      } 

      ProjectViewHandler handler = new ProjectViewHandler(this); 
      ProjectViewThread thread = new ProjectViewThread(handler, projectLang, projectSlug); 

      thread.start(); 
     } 
    } 

    @Override 
    public Object onRetainNonConfigurationInstance() { 
     return project; 
    } 

    public void setProject(I4pProjectTranslation p) { 
     project = p; 
    } 

    @TargetApi(11) 
    public void displayProject() { 
     setContentView(R.layout.projectview_description); 
     displayMenu = true; 
     if(Build.VERSION.SDK_INT >= 11) 
      invalidateOptionsMenu(); // Rebuild the menu 

     setTitle(project.getTitle()); 

     LinearLayout overlay = (LinearLayout) findViewById(R.id.projectview_description_overlay); 
     overlay.getBackground().setAlpha(127); 

     if(project.getProject().getPictures().size() > 0) { 
      ImageView image = (ImageView) findViewById(R.id.projectview_description_image); 
      image.setImageBitmap(project.getProject().getPictures().get(0).getImageBitmap()); 
     } 

     TextView title = (TextView) findViewById(R.id.projectview_description_title); 
     title.setText(project.getTitle()); 

     TextView baseline = (TextView) findViewById(R.id.projectview_description_baseline); 
     baseline.setText(project.getBaseline()); 

     TextView website = (TextView) findViewById(R.id.projectview_description_website); 
     if("".equals(project.getProject().getWebsite())) 
      website.setVisibility(View.GONE); 
     else 
      website.setOnClickListener(this); 

     if("".equals(project.getAboutSection())) { 
      LinearLayout aboutContainer = (LinearLayout) findViewById(R.id.projectview_description_about_container); 
      aboutContainer.setVisibility(View.GONE); 
     } else { 
      TextView aboutText = (TextView) findViewById(R.id.projectview_description_about_text); 
      aboutText.setText(project.getAboutSection()); 
     } 

     LinearLayout questions = (LinearLayout) findViewById(R.id.projectview_description_questions_container); 
     for(Question question : project.getProject().getQuestions()) { 
      if(question.getAnswer() != null) { 
       LinearLayout questionLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.projectview_question, null); 

       TextView questionView = (TextView) questionLayout.findViewById(R.id.projectview_question_question); 
       TextView answerView = (TextView) questionLayout.findViewById(R.id.projectview_question_answer); 

       questionView.setText(question.getQuestion()); 
       answerView.setText(question.getAnswer().trim()); 

       questions.addView(questionLayout); 
      } 
     } 
    } 

    public void onClick(View arg0) { 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(project.getProject().getWebsite())); 
     startActivity(intent); 
    } 
} 

Gracias por la ayuda!

Respuesta

8

Esto se debe a un error en el mecanismo de almacenamiento en caché del marco. La solución más fácil es usar un color que sea casi negro, como #ff010101.

+0

¿Cuál es la solución adecuada para esto? o diré si hay una solución adecuada para esto? ¿Y hay algo en el rastreador de errores de google? –

0

En lugar de definir el atributo de color negro en cada instancia del Diseño, ¿ha intentado establecer un estilo personalizado con ese atributo? Por un lado, es mejor para el mantenimiento, pero, por otro lado, tal vez el motor de estilo se maneje mejor con los cambios de configuración y el problema será resuelto.

+0

He intentado [crear un estilo] (http://paste.keuse.fr/view/75698231.html), pero no funciona, el fondo negro sigue místicamente en gris (como puede ver en capturas de pantalla agregadas a la primera publicación). Gracias por su ayuda :) – Kyriog

Cuestiones relacionadas