2012-10-01 22 views
5

Definisco esplicitamente un colore nero sullo sfondo del mio LinearLayout (usando @android: color/black nel mio XML), ma a volte (spesso dopo una rotazione dello schermo), il nero è trasformandosi in un colore grigio (o nero trasparente).Android - sfondo nero diventa grigio

Questo problema appare su molti dispositivi: sia su emulatore, Acer Liquid (Android 2.2) e Galaxy Nexus (Android 4.1).

Screenshots: Buggy view // Not buggy view

Ecco la mia XML e l'attività di codice:

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> 

attività:

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); 
    } 
} 

Grazie per l'aiuto!

risposta

8

Ciò è dovuto a un errore nel meccanismo di memorizzazione nella cache del framework. La soluzione più semplice consiste nell'utilizzare un colore quasi nero, ad esempio #ff010101.

+0

Qual è la correzione corretta per questo? o dovrei dire c'è una soluzione adeguata per questo? E c'è qualcosa nel bug tracker di Google? –

0

Invece di definire l'attributo del colore nero su ogni istanza del layout, hai provato a impostare uno stile personalizzato con quell'attributo? Da un lato, è meglio per la manutenzione, ma d'altra parte, forse il motore di stile si occupa meglio delle modifiche alla configurazione e il problema sarà risolto.

+0

Ho provato a [creare uno stile] (http://paste.keuse.fr/view/75698231.html), ma non funziona, lo sfondo nero ancora misticamente diventa grigio (come puoi vedere su screenshot aggiunti al 1 ° post). Grazie per l'aiuto :) – Kyriog

Problemi correlati