我的应用程序无法检测到白色

时间:2019-06-26 09:24:45

标签: android background-color relativelayout android-context

我有一个问题,但没人能帮助我。问题是: 我有详细的活动以显示我的产品的详细信息。 此活动具有一个白色背景的工具栏(相对布局)。 一切都很好,没有问题,但是当我从片段到细节活动时,我的工具栏颜色变成了蓝色! 我尝试检查是什么原因,最后我发现我的应用程序不知道白色!!如果我使用#ffffff(硬编码或从资源获取),我的工具栏会变成蓝色,但是如果使用其他任何颜色,即使#fffffe

也可以

这是我的风格

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/font</item>
        <item name="fontFamily">@font/font</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>




    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

详细活动代码:

public class DetailActivity extends AppCompatActivity {
    DetailViewModel detailViewModel = new DetailViewModel();
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    String id,title;
    TextView txtTitle, txtName, txtColor, txtguarantee, txtPrice, txtPoints, txtMore, txtDescription;
    ImageView imgImage, imgShare, imgFav, imgCart, imgMore, imgBack;
    RecyclerView recyclerView;
    CardView properties;
    CardView comments;
    RatingBar ratingBar;
    Button btnAddToBasket;
    List<RatingModel> ratingModels;
    NestedScrollView nestedScrollView;
    Thread thread;
    RelativeLayout toolbar;
    int toolbarMergeColor, drawableMergeColor;
    TextView txtToolbarTitle;
    float ratio;
    int startHeight;

    int toolbarTitleYPosition = -1;
    String imageUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        setupViews();
        getIntentId();
        txtToolbarTitle.setTranslationY(150);
       // observeForDetail();
    }

    private void getIntentId() {
        id = getIntent().getExtras().getString("id");
    }



    private void setupViews() {

        txtToolbarTitle = findViewById(R.id.txt_detail_toolbarTitle);
        imgImage = findViewById(R.id.img_detail_image);
        toolbar = findViewById(R.id.rel_detail_toolbar);




        ratingModels = new ArrayList<>();
        txtDescription = findViewById(R.id.txt_detail_description);
        imgShare = findViewById(R.id.img_detail_share);
        imgFav = findViewById(R.id.img_detail_favorite);
        imgBack = findViewById(R.id.img_detail_back);
        imgMore = findViewById(R.id.img_detail_more);
        imgMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu=new PopupMenu(DetailActivity.this,imgMore);
                popupMenu.getMenuInflater().inflate(R.menu.detail_more_menu,popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        if(menuItem.getItemId()==R.id.action_more_chart){
                            Intent intent=new Intent(getApplicationContext(), ChartActivity.class);
                            intent.putExtra("id",id);
                            intent.putExtra("title",title);
                            startActivity(intent);
                        }else{
                            Intent intent=new Intent(getApplicationContext(), CompareActivity.class);
                            intent.putExtra("image_url",imageUrl);
                            startActivity(intent);
                        }
                        return true;
                    }
                });

                popupMenu.show();
            }
        });
        imgCart = findViewById(R.id.img_detail_cart);
        txtTitle = findViewById(R.id.txt_detail_title);
        txtName = findViewById(R.id.txt_detail_name);
        txtColor = findViewById(R.id.txt_detail_color);
        txtguarantee = findViewById(R.id.txt_detail_guarantee);
        txtPrice = findViewById(R.id.txt_detail_price);
        txtPoints = findViewById(R.id.txt_detail_points);
        txtMore = findViewById(R.id.txt_detail_more);
        txtMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (txtMore.getText().toString().equals("ادامه مطلب")) {
                    startHeight = txtDescription.getHeight();
                    txtDescription.setMaxLines(Integer.MAX_VALUE);
                    int widthSpec = View.MeasureSpec.makeMeasureSpec(txtDescription.getWidth(), View.MeasureSpec.EXACTLY);
                    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    txtDescription.measure(widthSpec, heightSpec);
                    int targetHeight = txtDescription.getMeasuredHeight();
                    final int heightSpan = targetHeight - startHeight;
                    txtDescription.getLayoutParams().height = startHeight;
                    txtDescription.setLayoutParams(txtDescription.getLayoutParams());
                    Animation animation = new Animation() {
                        @Override
                        protected void applyTransformation(float interpolatedTime, Transformation t) {
                            txtDescription.getLayoutParams().height = (int) (startHeight + heightSpan * interpolatedTime);
                            txtDescription.setLayoutParams(txtDescription.getLayoutParams());
                        }
                    };
                    animation.setDuration(300);
                    txtDescription.startAnimation(animation);
                    txtMore.setText("بستن");
                } else {
                    startHeight = txtDescription.getHeight();
                    txtDescription.setMaxLines(8);
                    int widthSpec = View.MeasureSpec.makeMeasureSpec(txtDescription.getWidth(), View.MeasureSpec.EXACTLY);
                    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    txtDescription.measure(widthSpec, heightSpec);
                    int targetHeight = txtDescription.getMeasuredHeight();
                    final int heightSpan = targetHeight - startHeight;
                    txtDescription.getLayoutParams().height = startHeight;
                    txtDescription.setLayoutParams(txtDescription.getLayoutParams());
                    Animation animation = new Animation() {
                        @Override
                        protected void applyTransformation(float interpolatedTime, Transformation t) {
                            txtDescription.getLayoutParams().height = (int) (startHeight + heightSpan * interpolatedTime);
                            txtDescription.setLayoutParams(txtDescription.getLayoutParams());
                        }
                    };
                    animation.setDuration(300);
                    txtDescription.startAnimation(animation);
                    txtMore.setText("ادامه مطلب");
                }

            }
        });
        properties = findViewById(R.id.card_detail_properties);
        properties.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DetailActivity.this, PropertiesActivity.class);
                intent.putExtra("title", title);
                startActivity(intent);
            }
        });
        comments = findViewById(R.id.card_detail_comments);
        comments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), CommentActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("name",txtName.getText().toString());
                startActivity(intent);
            }
        });

        btnAddToBasket = findViewById(R.id.btn_detail_addToBasket);
        recyclerView = findViewById(R.id.rv_detail_points);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ratingBar = findViewById(R.id.rating_detail);


    }

    @Override
    protected void onDestroy() {
        compositeDisposable.dispose();
        super.onDestroy();
    }

}

xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@color/gray200"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Detail.DetailActivity">

    <RelativeLayout
        android:id="@+id/rel_detail_toolbar"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">


        <ImageView
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            android:id="@+id/img_detail_back"
            android:layout_alignParentRight="true"
            app:srcCompat="@drawable/ic_arrow_back_24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <ImageView
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            app:srcCompat="@drawable/ic_more_vert_black_24dp"
            android:id="@+id/img_detail_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:layout_centerVertical="true"
            app:srcCompat="@drawable/ic_shopping_cart_black_24dp"
            android:layout_toRightOf="@id/img_detail_more"
            android:id="@+id/img_detail_cart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_alignParentBottom="true"
            android:textColor="@color/colorWhite"
            android:text="test test"
            android:layout_marginRight="8dp"
            android:layout_toLeftOf="@id/img_detail_back"
            android:id="@+id/txt_detail_toolbarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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




            <ImageView
                android:background="#ffffff"
                android:id="@+id/img_detail_image"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <android.support.v7.widget.CardView
                app:cardBackgroundColor="@color/gray100"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RelativeLayout
                    android:paddingBottom="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/img_detail_share"
                        android:layout_margin="8dp"
                        app:srcCompat="@drawable/ic_share_black_24dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <ImageView
                        android:layout_margin="8dp"
                        app:srcCompat="@drawable/ic_favorite_black_24dp"
                        android:layout_toRightOf="@+id/img_detail_share"
                        android:id="@+id/img_detail_favorite"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:ellipsize="end"
                        android:layout_below="@id/img_detail_share"
                        android:maxLines="1"
                        android:minLines="1"
                        android:textColor="@color/colorBlack"
                        android:layout_marginRight="8dp"
                        android:layout_alignParentRight="true"
                        tools:text="هارد اکسترنال سیلیکن پاور 1 ترا بایت"
                        android:id="@+id/txt_detail_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_marginRight="8dp"
                        android:layout_alignParentRight="true"
                        tools:text="هارد اکسترنال سیلیکن پاور 1 ترا بایت"
                        android:textSize="12sp"
                        android:id="@+id/txt_detail_name"
                        android:layout_width="wrap_content"
                        android:layout_below="@id/txt_detail_title"
                        android:layout_height="wrap_content" />
                </RelativeLayout>
            </android.support.v7.widget.CardView>



            <LinearLayout
                android:background="@color/gray200"
                android:gravity="center"
                android:padding="16dp"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.CardView
                    android:layout_marginRight="4dp"
                    android:foreground="?android:selectableItemBackground"
                    android:layout_width="0dp"
                    android:id="@+id/card_detail_comments"
                    android:layout_weight="0.5"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:gravity="center"
                        android:padding="8dp"
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:textColor="@color/gray700"
                            android:layout_marginRight="8dp"
                            android:text="نظرات کاربران"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <ImageView
                            app:srcCompat="@drawable/ic_comment_black_24dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>
                <android.support.v7.widget.CardView
                    android:layout_marginLeft="4dp"
                    android:foreground="?android:selectableItemBackground"
                    android:layout_weight="0.5"
                    android:id="@+id/card_detail_properties"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:gravity="center"
                        android:padding="8dp"
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:textColor="@color/gray700"
                            android:layout_marginRight="8dp"
                            android:text="مشخصات"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <ImageView
                            app:srcCompat="@drawable/ic_comment_black_24dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>


            </LinearLayout>

            <android.support.v7.widget.CardView
                app:cardElevation="4dp"
                android:layout_marginRight="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginBottom="16dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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

                    <TextView
                        android:layout_marginRight="8dp"
                        android:textSize="16sp"
                        android:layout_gravity="right"
                        android:textColor="@color/colorBlack"
                        android:text="رنگ"
                        android:id="@+id/txt_detail_colorTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <TextView
                        android:layout_marginRight="8dp"
                        android:layout_gravity="right"
                        tools:text="مشکی"
                        android:id="@+id/txt_detail_color"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <TextView
                        android:layout_margin="8dp"
                        android:layout_gravity="right"
                        tools:text="گارانتی سه ساله ۀوا"
                        android:id="@+id/txt_detail_guarantee"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <View
                        android:background="@color/gray300"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"/>
                    <TextView
                        android:textColor="@color/red"
                        android:layout_margin="8dp"
                        android:layout_gravity="right"
                        android:text="فروش توسط دیجی کالا"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_margin="8dp"
                        android:layout_gravity="right"
                        android:text="آماده ارسال از انبار دیجی کالا"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <View
                        android:background="@color/gray300"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"/>

                    <TextView
                        android:id="@+id/txt_detail_price"
                        android:layout_margin="8dp"
                        android:textColor="@color/green"
                        tools:text="1230000 تومان"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <Button
                        android:id="@+id/btn_detail_addToBasket"
                        android:layout_margin="8dp"
                        android:textColor="@color/colorWhite"
                        android:text="افزودن به سبد خرید"
                        android:background="@drawable/shape_btn_add_to_basket"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:drawableLeft="@drawable/ic_keyboard_arrow_left_black_24dp"
                        android:drawablePadding="8dp"
                        android:drawableRight="@drawable/ic_account_balance_black_24dp"
                        android:layout_gravity="center"
                        android:text="8 فروشنده و گارانتی برای این کالا وجود دارد"
                        android:textColor="@color/colorAccent"
                        android:layout_width="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_height="wrap_content" />
                </LinearLayout>


            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                app:cardElevation="4dp"
                android:layout_margin="16dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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


                    <TextView
                        android:maxLines="6"
                        android:ellipsize="end"
                        android:padding="16dp"
                        android:textColor="@color/colorBlack"
                        tools:text="محصولی با دوام و با کیفیت عالی همراه با ضمانت نامه"
                        android:id="@+id/txt_detail_description"
                        android:layout_width="match_parent"
                        android:layout_height="170dp" />

                    <View
                        android:background="@color/gray200"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"/>

                    <TextView
                        android:id="@+id/txt_detail_more"
                        android:padding="8dp"
                        android:text="ادامه مطلب"
                        android:layout_gravity="center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>


            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                app:cardElevation="4dp"
                android:layout_margin="16dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RelativeLayout
                    android:padding="8dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <RatingBar
                        style="?android:attr/ratingBarStyleSmall"
                        android:layout_centerHorizontal="true"
                        android:progressTint="@color/red"
                        android:rating="3.5"
                        android:id="@+id/rating_detail"
                        android:numStars="5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <TextView
                        android:id="@+id/txt_detail_points"
                        android:layout_marginLeft="8dp"
                        android:layout_toRightOf="@id/rating_detail"
                        tools:text="3.5 از 5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <android.support.v7.widget.RecyclerView
                        android:layout_margin="8dp"
                        android:layout_below="@id/rating_detail"
                        android:id="@+id/rv_detail_points"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                </RelativeLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>


</LinearLayout>

4 个答案:

答案 0 :(得分:2)

什么是样式中的活动主题,如果您未设置noActionBar主题,则android会将默认工具栏放在相对布局上,而您看不到它

因此定义您的样式

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="colorPrimary">@color/activityBackground</item>
   <item name="colorPrimaryDark">@color/greyTextColor</item>
   <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
   <item name="windowActionBar">false</item>
   <item name="windowNoTitle">true</item>
</style>

并将AppTheme.NoActionBar栏主题设置为您在AndroidManifest文件中的活动 或使用

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

答案 1 :(得分:1)

尝试使用-

android:background =“ @ android:color / white”

答案 2 :(得分:1)

您可能想尝试一下;为“ colors.xml”定义一种新颜色,并将其用作背景。

更新

<item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

您应使用所需的颜色更改@color/colorPrimary@color/colorPrimaryDark。用颜色定义它们并分配它们,而不是上面的两个。它必须是固定的。祝你好运!

答案 3 :(得分:1)

您有两种选择

1)您可以在onCreate的明细活动中设置工具栏颜色

toolbar.setBackgroundColor(Color.WHITE);

检查这是否可行或

2)在color.xml中设置白色并从那里获取参考

#FFFFFFFF ”(删除多余的空格)

检查任何一项,如果不行,请通知我。