TShopping

 找回密碼
 註冊
搜索
查看: 4684|回復: 0
打印 上一主題 下一主題

[教學] ANDROID 背景如何動態改變

  [複製鏈接]
跳轉到指定樓層
1#
發表於 2016-3-3 23:48:10 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
Android要怎麼換背景, 有兩種方式, 一種是使用底色, 一種是使用圖片,
Android本身預設的顏色是黑色, 如果我們想要換底色,
首先在res->values之下新增一個xml檔案叫做color.xml
加入以下的程式碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <drawable name="darkgray">#808080</drawable>
  4.     <drawable name="white">#FFFFFF</drawable>
  5.     <drawable name="blue">#0000FF</drawable>
  6. </resources>
複製代碼

然後在你的main.xml(或者你的contentView的xml檔案)裡面加入
  1. <RelativeLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:background="@drawable/white"
  4.     android:orientation="vertical"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent">
  7. </RelativeLayout>
複製代碼

接著將這個main.xml設定為你的contentView就可以將背景換成白色的
  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4. }
複製代碼

如圖

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

然後在res裡面建立一個資料夾叫做drawable
以後如果有圖檔,就都丟進這個資料夾
接著只要改剛剛的main.xml即可,跟改顏色的方法一樣,只是把名稱換成圖片名稱而已
  1. <RelativeLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:background="@drawable/monkey"
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"
  8.     android:paddingRight="@dimen/activity_horizontal_margin"
  9.     android:paddingTop="@dimen/activity_vertical_margin"
  10.     android:paddingBottom="@dimen/activity_vertical_margin"
  11.     tools:context=".MainActivity">
  12. </RelativeLayout>
複製代碼

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"
  8.     android:paddingRight="@dimen/activity_horizontal_margin"
  9.     android:paddingTop="@dimen/activity_vertical_margin"
  10.     android:paddingBottom="@dimen/activity_vertical_margin"
  11.     android:id="@+id/my_ayout"
  12.     tools:context=".MainActivity">

  13.     <TextView
  14.         android:text="Hello World!"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"/>
  17. </RelativeLayout>
複製代碼

那如果你想要用程式碼來控制背景呢?
在改一下main.xml, 將LinearLayout多加上一個id屬性
然後在.java檔案裡面呼叫它就可以了
  1. public class MainActivity extends AppCompatActivity {
  2.     private RelativeLayout mLayout;
  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         setContentView(R.layout.activity_main);
  7.         findViewById(R.id.my_ayout);
  8.         mLayout = (RelativeLayout) findViewById(R.id.my_ayout);
  9.         Resources res = this.getResources();
  10.         Drawable drawable;
  11.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  12.             drawable = res.getDrawable(R.drawable.blue, getTheme());
  13.             mLayout.setBackground(drawable);
  14.         } else {
  15.             drawable = res.getDrawable(R.drawable.blue);
  16.             mLayout.setBackgroundDrawable(drawable);
  17.         }
  18.     }
  19. }
複製代碼

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



文章來源

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-25 10:11 , Processed in 0.058957 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表