GVKun编程网logo

reactjs-如何设置backgroundcolor的内联样式?(react background)

14

此处将为大家介绍关于reactjs-如何设置backgroundcolor的内联样式?的详细内容,并且为您解答有关reactbackground的相关问题,此外,我们还将为您介绍关于的backgrou

此处将为大家介绍关于reactjs-如何设置backgroundcolor的内联样式?的详细内容,并且为您解答有关react background的相关问题,此外,我们还将为您介绍关于的background-image和background-color层次问题_html/css_WEB-ITnose、android RecyclerView BackgroundColor、android setBackgroundResource setBackgroundColor setBackgroundResource区别、android – RecyclerView BackgroundColor的有用信息。

本文目录一览:

reactjs-如何设置backgroundcolor的内联样式?(react background)

reactjs-如何设置backgroundcolor的内联样式?(react background)

我想设置某些元素的样式属性,但是语法不正确。谁能建议我错了?

import React from ''react'';import debug from ''debug''const log = debug(''app:component:Header'');var bgColors = { "Default": "#81b71a",                    "Blue": "#00B1E1",                    "Cyan": "#37BC9B",                    "Green": "#8CC152",                    "Red": "#E9573F",                    "Yellow": "#F6BB42",};export default class SideBar extends React.Component {  constructor(props) {    super(props);  }  render() {    return (    <a>default</a>    <a>blue</a>    <a>cyan</a>    <a>green</a>    <a>red</a>    <a>yellow</a>           );  }}

更新:对于任何查看此内容的人,请查看注释,这不是有效的代码。

答案1

小编典典

https://facebook.github.io/react/tips/inline-
styles.html

您不需要引号。

<a style={{backgroundColor: bgColors.Yellow}}>yellow</a>

<body>的background-image和background-color层次问题_html/css_WEB-ITnose

的background-image和background-color层次问题_html/css_WEB-ITnose

body { background-image:url(pics/bg.png); background-repeat:repeat; background-color:#069;}
登录后复制

设置成这样,但背景颜色总是会覆盖在背景图片上,怎么能让背景图片在背景颜色上从而不受背景颜色影响?

回复讨论(解决方案)

你路径写错了呗。

背景图片,会覆盖背景色的,看下路径,在调试工具里,看下这个图片,是不是成功加载过来了。

图片路径不对吧

你路径写错了呗。

背景图片,会覆盖背景色的,看下路径,在调试工具里,看下这个图片,是不是成功加载过来了。
很确定背景图片已经加载了,和背景颜色叠加在一起出现了奇特的效果。

你确定加载了,那就看下,body下的第一个子元素,是不是占据整个body的,是不是你在这个子元素上面,设置了颜色。

再仔细检查下你的代码,颜色覆盖背景图片。还真是没有见过。

你把body的颜色去掉,看看会不会出背景色,就能确定,是不是背景图片被颜色覆盖了。

background:#069 url(pics/bg.png) no-repeat center center;

你的背景图片是png格式的,不要告诉我,撒是png这个你不懂

你的背景图片是png格式的,不要告诉我,撒是png这个你不懂
好吧,是图片半透明了

android RecyclerView BackgroundColor

android RecyclerView BackgroundColor

当我使用RecyclerView在我的应用程序中显示列表数据时,RecyclerView的backgroundcolor始终为白色:

screenshot

我在xml文件和代码中设置backgroundcolor,但它不起作用:

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/colorPrimary">
    <!--<include layout="@layout/content_main" />-->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

//fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:padding="5dp" />
</RelativeLayout>

//item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="@color/yellow"
    card_view:cardCornerRadius="1dp">

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

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:gravity="center"
            android:textColor="@color/text_white"
            android:textSize="@dimen/list_item_text_size" />

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:scaleType="centerCrop" />
    </RelativeLayout>

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


    //gradle file
    compileSdkVersion 23
    buildToolsversion "23.0.2"

    defaultConfig {
        applicationId "com.union.fmdouban"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

解决方法:

我已经实现了以下代码,这显示了卡片视图和背景color

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/appointment_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:scrollIndicators="none" />
</RelativeLayout>

//你的适配器的布局

  <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:padding="5dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:gravity="center"
            android:text="dfgjkdfh"
            android:textColor="#ffffff" />

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:scaleType="centerCrop" />
    </RelativeLayout>

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

我从适配器的XML中删除了主要的相对布局试试这个让我知道

//我的Gradle看起来像这样

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsversion "23.0.2"

    defaultConfig {
        applicationId "betasoft.com.tryso"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile filetree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:design:23.1.1' }

android setBackgroundResource setBackgroundColor setBackgroundResource区别

android setBackgroundResource setBackgroundColor setBackgroundResource区别

 总结一下:setBackgroundResource(R.color.XXX)改变的颜色是最下层的颜色,当改变完颜色以后,因为我头部布局在xml文件中默认颜色是white,所以又给white遮盖掉了。

       xml 文件中布局默认的颜色呢,可以理解为显示在中层。当我用Resource这个方法,把颜色改变过来以后,它因为在最下层,所以直接被中层的给刷掉了,因此颜色又变了回去。

       而setBackgroundColor(context.getResouce().getColor(R.color.XXX))呢,可以理解为改变的是最上层的颜色,不管我xml布局中的颜色是什么色,我在你布局颜色上层又给刷了回去。所以就显色了。

android – RecyclerView BackgroundColor

android – RecyclerView BackgroundColor

我使用RecyclerView显示一些文本数据.我在其中选择了不同的卡片.

我想改变所选卡片的外观.

public void toggleSelection(int pos)
    {
        RecyclerView.ViewHolder viewHolder = recView.findViewHolderForPosition(pos);
        if (selectedItems.get(pos, false)) {
            selectedItems.delete(pos);
            viewHolder.itemView.setBackgroundColor(Color.WHITE);
        }
        else {
            selectedItems.put(pos, true);
            viewHolder.itemView.setBackgroundColor(Color.GREEN);
        }
        notifyItemChanged(pos);
    }

如果我像这样使用我的代码它是有效的.我的onClick事件触发此代码,我的卡背景颜色变为绿色.

所以这是我的问题:向下滚动显示其他卡片在相同的相对位置(但在列表中更下方),即使没有被选中,也会使用相同的背景颜色;选择第一张卡并向下滚动到第八张卡是顶部可见卡的位置,显示第八张卡突出显示.

解决方法:

您需要在onBindViewHolder()方法中显式设置颜色.回收站视图的名称建议回收视图,因此第0项将被回收为您案例中的第8项.它们使用使用onCreateViewHolder()方法创建的相同视图持有者.每当其中任何一个进入视图时,都会调用onBindViewHolder()方法.
我建议你在数据模型中创建一个额外的布尔字段,告诉你是否突出显示了视图.你应该在toggleSelection()中切换它.
然后在onBindViewHolder()中检查该字段的值并相应地设置颜色.

今天的关于reactjs-如何设置backgroundcolor的内联样式?react background的分享已经结束,谢谢您的关注,如果想了解更多关于的background-image和background-color层次问题_html/css_WEB-ITnose、android RecyclerView BackgroundColor、android setBackgroundResource setBackgroundColor setBackgroundResource区别、android – RecyclerView BackgroundColor的相关知识,请在本站进行查询。

本文标签: