Android – 黑色背景变成灰色

Android – 黑色背景变成灰色,第1张

概述我明确地为我的LinearLayout的背景定义了一种黑色(在我的 XML中使用@android:color / black),但有时(通常在屏幕旋转后),黑色变成灰色(或透明的黑色) ). 此问题出现在许多设备上:模拟器,Acer Liquid(Android 2.2)和Galaxy Nexus(Android 4.1). 截图:Buggy view // Not buggy view 这是我的 我明确地为我的linearLayout的背景定义了一种黑色(在我的 XML中使用@androID:color / black),但有时(通常在屏幕旋转后),黑色变成灰色(或透明的黑色) ).

此问题出现在许多设备上:模拟器,Acer liquID(Android 2.2)和galaxy Nexus(AndroID 4.1).

截图:Buggy view // Not buggy view

这是我的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,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);    }}

感谢帮助!

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

以上是内存溢出为你收集整理的Android – 黑色背景变成灰色全部内容,希望文章能够帮你解决Android – 黑色背景变成灰色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存