6

我明确地为我的 LinearLayout 的背景定义了黑色(在我的 XML 中使用 @android:color/black),但有时(通常在屏幕旋转之后),黑色变成灰色(或透明黑色)。

这个问题出现在许多设备上:模拟器、Acer Liquid (Android 2.2) 和 Galaxy Nexus (Android 4.1)。

截图:错误视图//不是错误视图

这是我的 XML 和活动代码:

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>

活动:

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

感谢帮助!

4

2 回答 2

8

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

于 2013-01-06T17:40:25.697 回答
0

您是否尝试过使用该属性设置自定义样式,而不是在布局的每个实例上定义黑色属性?一方面,它对维护更好,但另一方面,也许样式引擎更好地处理配置更改,问题就会得到解决。

于 2012-10-01T11:02:04.703 回答