如何動態更換背景

如何動態更換背景

情境

Android要怎麼換背景
有兩種方式

  • 一種是使用底色
  • 一種是使用圖片

程式碼說明

首先在res->values之下新增一個xml檔案叫做color.xml
加入以下的程式碼

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="darkgray">#808080</drawable>
    <drawable name="white">#FFFFFF</drawable>
    <drawable name="blue">#0000FF</drawable>
</resources>

然後在你的main.xml(或者你的contentView的xml檔案)裡面加入

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

接著將這個main.xml設定為你的contentView就可以將背景換成白色的

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

如圖

接下來是把自己想要的圖片變成背景
首先選好一張你想要得圖片,
假設是這張

然後在res裡面建立一個資料夾叫做drawable
以後如果有圖檔,就都丟進這個資料夾

接著只要改剛剛的main.xml即可,跟改顏色的方法一樣,只是把名稱換成圖片名稱而已

<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/monkey"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
</RelativeLayout>

然後你就可以看到下面的畫面

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/my_ayout"
    tools:context=".MainActivity">

    <TextView
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

那如果你想要用程式碼來控制背景呢?
在改一下main.xml, 將LinearLayout多加上一個id屬性

然後在.java檔案裡面呼叫它就可以了

public class MainActivity extends AppCompatActivity {
    private RelativeLayout mLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.my_ayout);
        mLayout = (RelativeLayout) findViewById(R.id.my_ayout);
        Resources res = this.getResources();
        Drawable drawable;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable = res.getDrawable(R.drawable.blue, getTheme());
            mLayout.setBackground(drawable);
        } else {
            drawable = res.getDrawable(R.drawable.blue);
            mLayout.setBackgroundDrawable(drawable);
        }
    }
}

這樣一來就可以看到當機的畫面了XDDD

程式碼