Android – 黑色背景变成灰色

前端之家收集整理的这篇文章主要介绍了Android – 黑色背景变成灰色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我明确地为我的LinearLayout的背景定义了一种黑色(在我的 XML中使用@android:color / black),但有时(通常在屏幕旋转后),黑色变成灰色(或透明的黑色) ). @H_301_2@此问题出现在许多设备上:模拟器,Acer Liquid(Android 2.2)和Galaxy Nexus(Android 4.1).

@H_301_2@截图:Buggy view // Not buggy view

@H_301_2@这是我的XML和活动代码

@H_301_2@XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10.  
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="150dp"
  14. android:minHeight="150dp">
  15.  
  16. <ImageView
  17. android:id="@+id/projectview_description_image"
  18. android:layout_width="match_parent"
  19. android:layout_height="150dp"
  20. android:contentDescription="@string/projectview_description_image_description"
  21. android:scaleType="centerCrop"
  22. android:src="@drawable/project_nophoto" />
  23.  
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_alignParentBottom="true"
  28. android:orientation="vertical"
  29. android:id="@+id/projectview_description_overlay"
  30. android:paddingTop="5dp"
  31. android:paddingLeft="10dp"
  32. android:paddingBottom="5dp"
  33. android:background="@android:color/black"
  34. android:height="35dp">
  35.  
  36. <TextView
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:id="@+id/projectview_description_title"
  40. android:textAllCaps="true"
  41. android:textColor="@android:color/white"
  42. android:textSize="20dp"
  43. android:textStyle="bold"/>
  44.  
  45. <TextView
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:id="@+id/projectview_description_baseline"
  49. android:textColor="@android:color/white"
  50. android:textStyle="italic" />
  51.  
  52. </LinearLayout>
  53.  
  54. </RelativeLayout>
  55.  
  56. <!-- This LinearLayout background is buggy -->
  57. <LinearLayout
  58. android:layout_width="match_parent"
  59. android:layout_height="40dp"
  60. android:orientation="horizontal"
  61. android:background="@android:color/black">
  62.  
  63. <ImageView
  64. android:layout_width="175dp"
  65. android:layout_height="25dp"
  66. android:layout_gravity="center_vertical"
  67. android:paddingLeft="10dp"
  68. android:paddingRight="10dp"
  69. android:scaleType="fitCenter"
  70. android:src="@drawable/project_status_finish" />
  71.  
  72. <TextView
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:layout_gravity="center_vertical"
  76. android:gravity="right"
  77. android:paddingRight="10dp"
  78. android:text="@string/projectview_description_website"
  79. android:textColor="@color/website_link"
  80. android:textSize="15dp"
  81. android:id="@+id/projectview_description_website" />
  82.  
  83. </LinearLayout>
  84.  
  85. <LinearLayout
  86. android:layout_width="match_parent"
  87. android:layout_height="wrap_content"
  88. android:orientation="vertical"
  89. android:id="@+id/projectview_description_about_container"
  90. android:padding="10dp"
  91. android:paddingBottom="0dp">
  92.  
  93. <TextView
  94. android:layout_width="match_parent"
  95. android:layout_height="wrap_content"
  96. android:text="@string/projectview_description_about"
  97. android:textAllCaps="true"
  98. android:textStyle="bold" />
  99.  
  100. <TextView
  101. android:layout_width="match_parent"
  102. android:layout_height="wrap_content"
  103. android:id="@+id/projectview_description_about_text" />
  104.  
  105. </LinearLayout>
  106.  
  107. <LinearLayout
  108. android:layout_width="match_parent"
  109. android:layout_height="wrap_content"
  110. android:orientation="vertical"
  111. android:id="@+id/projectview_description_questions_container" />
  112.  
  113. </LinearLayout>
  114.  
  115. </ScrollView>
@H_301_2@活动:

  1. public class ProjectViewActivity extends Activity implements OnClickListener {
  2. private boolean displayMenu = false;
  3. private Intent shareIntent;
  4. private Favoritesqlite db;
  5. private I4pProjectTranslation project;
  6.  
  7. @TargetApi(14)
  8. @Override
  9. public boolean onPrepareOptionsMenu(Menu menu) {
  10. if(displayMenu) {
  11. // Inflate menu only if it hasn't been done before
  12. if(menu.size() == 0) {
  13. // Inflating the menu
  14. MenuInflater inflater = getMenuInflater();
  15. inflater.inflate(R.menu.projectview,menu);
  16.  
  17. // Creating share intent
  18. Intent prepareShareIntent = new Intent(Intent.ACTION_SEND);
  19. prepareShareIntent.putExtra(Intent.EXTRA_TEXT,UriHelper.getProjectUrl(project));
  20. prepareShareIntent.putExtra(Intent.EXTRA_SUBJECT,project.getTitle());
  21. prepareShareIntent.setType("text/plain");
  22. shareIntent = Intent.createChooser(prepareShareIntent,getResources().getText(R.string.projectview_menu_share_dialog));
  23. }
  24.  
  25. // Defining favorite state
  26. MenuItem favoriteItem = menu.getItem(0);
  27. if(db.isFavorite(project))
  28. favoriteItem.setTitle(R.string.projectview_menu_favorites_remove);
  29. else
  30. favoriteItem.setTitle(R.string.projectview_menu_favorites_add);
  31. }
  32. return super.onCreateOptionsMenu(menu);
  33. }
  34.  
  35. @Override
  36. public boolean onOptionsItemSelected(MenuItem item) {
  37. switch(item.getItemId()) {
  38. case android.R.id.home:
  39. if(getIntent().getData() != null) {
  40. Intent intent = new Intent(this,HomepageActivity.class);
  41. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  42. startActivity(intent);
  43. } else
  44. finish();
  45. break;
  46. case R.id.projectview_favorite:
  47. Toast t;
  48. if(db.isFavorite(project)) {
  49. db.removeFavorite(project);
  50. t = Toast.makeText(this,getResources().getString(R.string.projectview_toast_favorites_remove,project.getTitle()),Toast.LENGTH_SHORT);
  51. } else {
  52. db.addFavorite(project);
  53. t = Toast.makeText(this,getResources().getString(R.string.projectview_toast_favorites_add,Toast.LENGTH_SHORT);
  54. }
  55. t.show();
  56. break;
  57. case R.id.projectview_share:
  58. startActivity(shareIntent);
  59. break;
  60. }
  61. return super.onOptionsItemSelected(item);
  62. }
  63.  
  64. @TargetApi(11)
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. if(Build.VERSION.SDK_INT < 11)
  69. requestWindowFeature(Window.FEATURE_NO_TITLE);
  70. setContentView(R.layout.loading);
  71. db = new Favoritesqlite(this);
  72.  
  73. if(Build.VERSION.SDK_INT >= 11)
  74. getActionBar().setDisplayHomeAsUpEnabled(true);
  75.  
  76. project = (I4pProjectTranslation) getLastNonConfigurationInstance();
  77. if(project != null)
  78. displayProject();
  79. else {
  80. String projectLang;
  81. String projectSlug;
  82.  
  83. Uri data = getIntent().getData();
  84. if(data != null) {
  85. List<String> path = data.getPathSegments();
  86. projectLang = path.get(0);
  87. projectSlug = path.get(2);
  88. } else {
  89. Bundle extras = getIntent().getExtras();
  90.  
  91. if(extras.containsKey("project_title"))
  92. setTitle(extras.getString("project_title"));
  93.  
  94. projectLang = extras.getString("project_lang");
  95. projectSlug = extras.getString("project_slug");
  96. }
  97.  
  98. ProjectViewHandler handler = new ProjectViewHandler(this);
  99. ProjectViewThread thread = new ProjectViewThread(handler,projectLang,projectSlug);
  100.  
  101. thread.start();
  102. }
  103. }
  104.  
  105. @Override
  106. public Object onRetainNonConfigurationInstance() {
  107. return project;
  108. }
  109.  
  110. public void setProject(I4pProjectTranslation p) {
  111. project = p;
  112. }
  113.  
  114. @TargetApi(11)
  115. public void displayProject() {
  116. setContentView(R.layout.projectview_description);
  117. displayMenu = true;
  118. if(Build.VERSION.SDK_INT >= 11)
  119. invalidateOptionsMenu(); // Rebuild the menu
  120.  
  121. setTitle(project.getTitle());
  122.  
  123. LinearLayout overlay = (LinearLayout) findViewById(R.id.projectview_description_overlay);
  124. overlay.getBackground().setAlpha(127);
  125.  
  126. if(project.getProject().getPictures().size() > 0) {
  127. ImageView image = (ImageView) findViewById(R.id.projectview_description_image);
  128. image.setImageBitmap(project.getProject().getPictures().get(0).getImageBitmap());
  129. }
  130.  
  131. TextView title = (TextView) findViewById(R.id.projectview_description_title);
  132. title.setText(project.getTitle());
  133.  
  134. TextView baseline = (TextView) findViewById(R.id.projectview_description_baseline);
  135. baseline.setText(project.getBaseline());
  136.  
  137. TextView website = (TextView) findViewById(R.id.projectview_description_website);
  138. if("".equals(project.getProject().getWebsite()))
  139. website.setVisibility(View.GONE);
  140. else
  141. website.setOnClickListener(this);
  142.  
  143. if("".equals(project.getAboutSection())) {
  144. LinearLayout aboutContainer = (LinearLayout) findViewById(R.id.projectview_description_about_container);
  145. aboutContainer.setVisibility(View.GONE);
  146. } else {
  147. TextView aboutText = (TextView) findViewById(R.id.projectview_description_about_text);
  148. aboutText.setText(project.getAboutSection());
  149. }
  150.  
  151. LinearLayout questions = (LinearLayout) findViewById(R.id.projectview_description_questions_container);
  152. for(Question question : project.getProject().getQuestions()) {
  153. if(question.getAnswer() != null) {
  154. LinearLayout questionLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.projectview_question,null);
  155.  
  156. TextView questionView = (TextView) questionLayout.findViewById(R.id.projectview_question_question);
  157. TextView answerView = (TextView) questionLayout.findViewById(R.id.projectview_question_answer);
  158.  
  159. questionView.setText(question.getQuestion());
  160. answerView.setText(question.getAnswer().trim());
  161.  
  162. questions.addView(questionLayout);
  163. }
  164. }
  165. }
  166.  
  167. public void onClick(View arg0) {
  168. Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(project.getProject().getWebsite()));
  169. startActivity(intent);
  170. }
  171. }
@H_301_2@感谢帮助!

解决方法

这是由于框架的缓存机制中的错误.最简单的解决方法是使用几乎为黑色的颜色,例如#ff010101.

猜你在找的Android相关文章