Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Android Background Color and Background Image

Android Background Color and Background Image บทความนี้เราจะมารู้วิธีการเขียน Android และการใส่ Background Color สีพื้นหลัง และ Background Image พื้นหลังแบบรูปภาพ แสดงบน Activity ต่าง ๆ ด้วยิธีการง่าย ๆ

Android Background Color and Background Image

Android Background Color and Background Image


ในการสร้าง Background แบบ Color และ Background Image บน Activity นั้นสามารถทำได้ง่าย ๆ ด้วยการเรียกค่าตัวแปรของ Color หรือ เรียกชื่อไฟล์รูปภาพได้ในทันที เรามาดูตัวอย่างกัน

Android Background Color

Android Background Color and Background Image

โครงสร้างไฟล์ดังรูป โดบเราจะใช้หลัก ๆ 3 ไฟล์คือ MainActivity.java , activity_main.xml และ string.xml

/res/value/string.xml

Android Background Color and Background Image

สร้างตัวแปรในไฟล์ string.xml

<resources>

    <string name="app_name">MyApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">My App V 1.0</string>
    
    <color name="yellow">#EFE100</color>

</resources>


activity_main.xml การเรียกใช้

Android Background Color and Background Image

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow">

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="130dp"
        android:text="www.ThaiCreate.Com"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


Android Background Color and Background Image

ภาพบน Layout ที่ได้

Android Background Color and Background Image

ทดสอบบน Emulator จะได้ Background Color บน Activity ดังรูป









Android Background Image

Android Background Color and Background Image

อันนี้เป็นไฟล์รูปภาพ Background Image

Android Background Color and Background Image

ให้ Copy ไฟล์ไปไว้ดังภาพ ถ้าไม่มีโฟเดอร์ให้สร้างขึ้นมาใหม่

activity_main.xml การเรียกใช้

Android Background Color and Background Image

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="130dp"
        android:text="www.ThaiCreate.Com"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


Android Background Color and Background Image

หน้าจอ XML Layout ที่ได้ ส่วน Code ของ Java ยังไม่ได้ทำอะไรเพิ่มเริม

MainActivity.java
package com.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


Screenshot

Android Background Color and Background Image

แสดง Background Image บนหน้าจอ







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-13 09:43:37 / 2017-03-26 20:28:04
  Download : No files
 Sponsored Links / Related

 
รู้จัก Palette หรือเครื่องมือ Widgets Element ต่าง ๆ ที่อยู่ใน Android และ Eclipse
Rating :

 
Android Form Widgets
Rating :

 
Android Text Fields Widgets
Rating :

 
Android Layouts Widgets
Rating :

 
Android Composite Widgets
Rating :

 
Android Images & Media Widgets
Rating :

 
Android Time & Date Widgets
Rating :

 
Android Transitions Widgets
Rating :

 
Android Advanced Widgets
Rating :

 
Android Image Resource from URL Website
Rating :

 
Android Thumbnail from Gallery
Rating :

 
Android java.lang.OutOfMemoryError (แก้ปัญหาเรื่อง OutOfMemoryError ในการแสดงรูปภาพ)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่