ฉันต้องการที่จะเปลี่ยนสีพื้นหลังเป็นสีขาวในแอพ Android ของฉันในวิธีที่ง่ายที่สุดที่เป็นไปได้
ฉันต้องการที่จะเปลี่ยนสีพื้นหลังเป็นสีขาวในแอพ Android ของฉันในวิธีที่ง่ายที่สุดที่เป็นไปได้
คำตอบ:
คุณต้องใช้ Android: คุณสมบัติพื้นหลังเช่น
android:background="@color/white"
นอกจากนี้คุณต้องเพิ่มค่าสำหรับสีขาวใน strings.xml
<color name="white">#FFFFFF</color>
แก้ไข: 18 พฤศจิกายน 2555
ตัวอักษรสองตัวแรกของรหัสสีตัวอักษร 8 ตัวให้ค่าอัลฟาถ้าคุณกำลังใช้สัญกรณ์สีตัวอักษร html 6 ตัวอักษรสีทึบแสง
เช่น :

android:background="@android:color/white"ซึ่งกำหนดไว้ล่วงหน้าและไม่ต้องการให้คุณเพิ่มอะไรลงใน strings.xml
                    /res/layout/activity_main.xmlสำหรับเพิ่มองค์ประกอบandroid:backgroundในคำตอบได้หรือไม่?
                    คุณยังสามารถใช้
android:background="#ffffff"
ในรูปแบบ xml ของคุณ/res/layout/activity_main.xmlหรือคุณสามารถเปลี่ยนธีมใน AndroidManifest.xml ของคุณโดยการเพิ่ม  
android:theme="@android:style/Theme.Light"
ไปยังแท็กกิจกรรมของคุณ
หากคุณต้องการเปลี่ยนพื้นหลังแบบไดนามิกให้ใช้
YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));
              ในการเปลี่ยนสีพื้นหลังด้วยวิธีที่ง่ายที่สุดเท่าที่จะทำได้ทางโปรแกรม (เฉพาะ - ไม่มีการเปลี่ยนแปลง XML):
LinearLayout bgElement = (LinearLayout) findViewById(R.id.container);
bgElement.setBackgroundColor(Color.WHITE);
ข้อกำหนดเพียงอย่างเดียวคือองค์ประกอบ "ฐาน" ของคุณใน activity_whething.xml มีรหัสที่คุณสามารถอ้างอิงใน Java ( containerในกรณีนี้):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
     ...
</LinearLayout>
Paschalis และ James ที่ตอบกลับมาแล้วนำฉันไปสู่โซลูชันนี้หลังจากตรวจสอบความเป็นไปได้ต่าง ๆ ในวิธีตั้งค่าสีข้อความของ TextView ในรหัส .
หวังว่ามันจะช่วยให้ใครบางคน!
วิธีนี้ใช้ได้ผลสำหรับฉัน:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.layout.rootLayout);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.bg_color_2));
ตั้งค่า id ในโครงร่าง xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="@color/background_color"
เพิ่มค่าสี / color.xml
<color name="bg_color_2">#ffeef7f0</color>
              วิธีที่ง่ายที่สุดคือการเพิ่มandroid:background="#FFFFFF"ไปยังโหนดหลักใน layout.xml หรือ/res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="10dp"
       android:textSize="20sp" 
       android:background="#FFFFFF">
   </TextView>
              คุณสามารถลองในxmlแผ่นงาน:
android:background="@color/background_color"
              ในเค้าโครงเพื่อเปลี่ยนพื้นหลังให้ใช้สิ่งนี้
android:background="@color/your_color"
ในโปรแกรมสามารถใช้สิ่งนี้ ตัวอย่างเช่น: สีพื้นหลังของ Texview
 TextView tvName = (TextView) findViewById(R.id.tvName);
 tvName.setBackgroundColor(getResources().getColor(R.color.your_color));
              คุณสามารถลองด้วยวิธีนี้
android:background="@color/white"
              อีกวิธีหนึ่งไปที่ โครงร่าง -> ไฟล์. xmlของคุณ-> มุมมองออกแบบจากนั้นไปที่แผนผังองค์ประกอบและเลือกโครงร่างที่คุณต้องการเปลี่ยนสี ในโครงสร้างด้านล่างมีส่วนคุณสมบัติเลือกพื้นหลังในส่วนคุณสมบัติ (ในส่วนรูปภาพ 1) จากนั้นคลิกส่วนที่ 2 ในภาพ จากนั้น หน้าต่างทรัพยากรจะเปิดขึ้นจากนั้นเลือกเมนูสีจากนั้นเลือกสีที่คุณต้องการ ป้อนคำอธิบายรูปภาพที่นี่
คุณสามารถใช้ทรัพยากรสีอย่างง่ายซึ่งโดยปกติจะระบุไว้ภายใน
res/values/colors.xml.
ใช้
<color name="red">#ffff0000</color>
android:background="@color/red"และใช้นี้ผ่านทาง สีนี้สามารถใช้ที่อื่นได้เช่นสีตัวอักษร อ้างอิงใน XML ด้วยวิธีเดียวกันหรือรับเป็นรหัสผ่าน
getResources().getColor(R.color.red).
คุณยังสามารถใช้ทรัพยากร drawable ใด ๆ เป็นพื้นหลังใช้ android:background="@drawable/mydrawable"สำหรับสิ่งนี้ (ซึ่งหมายถึง 9patch drawables, บิตแมปปกติ, drawable รูปร่าง, .. )
บางครั้งข้อความมีสีเดียวกันกับพื้นหลังลองใช้ android: background = "# CCCCCC" ในคุณสมบัติ listview และคุณจะเห็นได้ว่า
android: background = "# 64B5F6"
 
คุณสามารถเปลี่ยนค่าหลังจาก '#' ตามข้อกำหนดของคุณเองหรือต้องการขึ้นอยู่กับวิธีที่คุณต้องการใช้ 
นี่คือตัวอย่างรหัส:
<TextView
        android:text="Abir"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:background="#D4E157"  />
ขอบคุณ :)
เขียนรหัสนี้ขึ้นมา:
android:background="@color/colorAccent"หากคุณต้องการเพิ่มสีพื้นหลังให้กับกิจกรรมทั้งหมด
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1de9b6"
    tools:context="com.example.abc.myapplication.MainActivity">
 </RelativeLayout>
หากคุณต้องการใช้พื้นหลังสำหรับมุมมอง
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Andrios"
    android:background="@color/colorAccent" />
หวังว่านี่จะช่วยได้!
รหัสนี้มีประโยชน์สำหรับคุณ:
android:background="#fff"
              ฉันต้องการที่จะเปลี่ยนสีพื้นหลังเป็นสีขาวในแอพ Android ของฉันในวิธีที่ง่ายที่สุดที่เป็นไปได้
ตั้งค่าparentViewStyleในมุมมองพาเรนต์ทั้งหมดของคุณ ชอบมุมมองพาเรนต์ส่วนใหญ่ของกิจกรรมชิ้นส่วนและกล่องโต้ตอบ
<LinearLayout style="@style/parentViewStyle">
  ... other components inside
</LinearLayout>
เพียงแค่ใส่สไตล์นี้ไว้ข้างใน res>values>styles.xml
<style name="parentViewStyle">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:background">@color/white</item> // set your color here.
    <item name="android:orientation">vertical</item>
</style>
ด้วยวิธีนี้คุณไม่จำเป็นต้องเปลี่ยนสีพื้นหลังหลายครั้งในอนาคต
สำหรับKotlinและไม่เพียง แต่เมื่อคุณเขียน
@สี/
คุณสามารถเลือกสิ่งที่คุณต้องการได้อย่างรวดเร็วและง่ายดาย:
android:background="@color/md_blue_900"