android動態設置背景
A. android怎麼動態設置背景顏色
可以用selector。
<selector xmlns:android="http://">
<item
android:state_pressed="true"
android:background="@drawable/pressed"
/>
<item
android:state_selected="false"春漏
android:state_focused="false"
android:state_pressed="false"
android:background="@drawable/unselected"扒正爛
/>
</selector>
具體用法清沖google一下。
B. android中怎麼實現動態設置背景圖片的功能,我在網上搜到的只能設置到當前的頁面,並不能實現設置到全部
當關閉重新運行,它又會自動跳回原始的背景圖片。
在開發過程中,由於使用模擬器測試了程序,在首次運行後會將res文件夾下的圖片資源文件(如drawable-hdpi、drawable-ldpi和drawable-mdpi)拷貝到bin文件夾下。在替換資源圖片後,eclipse並不清楚是否有圖片改變,所以會使用原來bin下的res文件夾中的資源文件進行打包,而圖片用的還是第一次eclipse所拷貝進去的文件,所以當運行程序後會發現替換資源圖片在程序中沒起作用。
解決辦法:每次運行前,清理項目
動態設置背景圖片代碼
privateinti=0;//全局變數定義,初始化
//list數組接收到從文件中讀取到的數據
List<String>list=readTxt.getDierguanResource();
//changeBack這個函數用來動態設置背景圖片
publicvoidchangeBack(intbackground){
main=(LinearLayout)findViewById(R.id.shizi);
Stringa=list.get(background);
//獲取到的背景圖片名as(圖片存到res/drawable文件下)
Stringas=a.split("")[1];
//動態獲取圖片getResources().getIdentifier(as,"drawable",getPackageName())
intresID=getResources().getIdentifier(as,"drawable",getPackageName());
//設置頁面背景setBackgroundResource()
main.setBackgroundResource(resID);
}
if(i>=0&&i<list.size()){
changeBack(i);
}
C. android設置背景圖片
教你如何設置背景圖片
xml文件設置背景圖片中:
任意一個控制項,button imageView 或layout,在其的xml屬性設置中,添加
[java] view plain
android:background="@drawable/bg"
即可實現設置其背景圖片為bg.
其中bg.bnp圖片存放在drawable目錄下。
drawable目錄下存放大小圖標共用的圖片。drawable-hdpi中存放240 WVGA800 的模擬器或板子用的圖片。drawable-mdpi存放對應的小圖片
[java] view plain
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic"/>
同樣,用src屬性,也可設置imageView的圖片為pic.png
java代碼中設置或更改一個控制項的背景圖片:
[java] view plain
Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);
程序中,動態修改layout的背景圖片,用setBackgroundDrawable()函數實現。設置其背景圖片為 layout_bg.png
修改imageView1的背景圖片為imageView2的背景圖片:
imageView2.getDrawable()可獲得圖片2的背景。值為Drawable類型
imageView1.setImageDrawable(drawable); 設置imageView1的背景圖片
即:
[java] view plain
imageView1.setImageDrawable(imageView2.getDrawable());
D. Android開發,動態設置Activity的layout背景圖片問題
Bitmap img = BitmapFactory.decodeFile(BackgroundPath);
執行這行代碼的時候,拋出的異常,估計是報內存溢出
原因很簡單,Bitmap 是用位元組來代碼像素點的
如果你的圖片非常大,那麼android需要創建一個非常大的數組來生成bitmap對象
這時候就會拋出異常
建議你創建Bitmap前,先測試一下圖片的大小,把圖片的大小壓縮成屏蔽的大小
再用來做layout的Background
E. android動態背景圖片改變
Button setIcon;
boolean isIconChange = true;
//-----------------------------------------------
setIcon.setBackgroundResource(R.drawable.bg1); //setIcon定義為全局
isIconChange = false;//定義為全局
setIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(isIconChange){
setIcon.setBackgroundResource(R.drawable.bg1);
isIconChange = false;
}else{
setIcon.setBackgroundResource(R.drawable.bg2);
isIconChange = true;
}
}
});
就行了 ,不用放在xml裡面。
F. android 實現動態背景 用gif好么
1、下載安裝AnimGIF Live Wallpaper,值得注意的是,安裝成功之後,在應用程序列表是無法找到它的圖標,找到方式看下圖: 2、進入AnimGIF Live Wallpaper,點擊左下方的設置按鈕,選擇「Set GIF Image」按鈕即進入文件目錄瀏覽,找到需要設置為壁紙的GIF動圖,點擊設置壁紙即可。 注意事項: 1、GIF動圖體積不要過大,盡量不要超過1M,否則會出現卡頓、掉幀等播放問題; 2、設置動圖為壁紙後,會導致耗電量的增加。
G. android 動態設置按鈕背景的漸變顏色
在一個xml文件中定義需要用到gradient,然後用drawable設置,大致是這樣
H. 如何通過代碼動態改變android的窗口背景顏色
Android修改背景,邊框等代碼如下:
<?xmlversion="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android">
<!--圓角-->
<corners
android:radius="9dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/><!--設置圓角半徑御答-->
<!--漸變-->
<gradient
野拆襪android:startColor="@android:color/white"
android:centerColor="@android:color/black"
頌激android:endColor="@android:color/black"
android:useLevel="true"
android:angle="45"
android:type="radial"
android:centerX="0"
android:centerY="0"
android:gradientRadius="90"/>
<!--間隔-->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!--各方向的間隔-->
<!--大小-->
<size
android:width="50dp"
android:height="50dp"/><!--寬度和高度-->
<!--填充-->
<solid
android:color="@android:color/white"/><!--填充的顏色-->
<!--描邊-->
<stroke
android:width="2dp"
android:color="@android:color/black"
android:dashWidth="1dp"
android:dashGap="2dp"/>
</shape>
I. android studio 怎樣讓app背景圖為動態的
方法/步驟
1
首先打開軟體,看看現在的背景是什麼樣子,如圖默認是windows主題
2
在菜單欄中的file欄下選擇「settings」,並找到外觀「appearance」。
3
修改主題「theme」,軟體自帶三種可選主題。
4
主題一:Darcula,就是當今最流行的背景主題
5
主題二:Intellij,灰白系列,相比第三個windows主題更好看一些,但個人還是覺得Darcula最好。
6
確定好主題後,選擇「restart」重啟軟體。就可以看到這亮麗的背景,頓時覺得高大上了有木有。
J. android怎樣動態設置toolbar背景
首先使用 Toolbar 來代替ActionBar
,這樣我們就能夠把ActionBar嵌入到我們的View體系中,然後我們"禁用"系統的status bar,由 DrawerLayout
來處理status bar,最後抽屜部分往上移,或者裁剪掉status bar那一部分。
控制Status bar
在你的values-v21裡面添加新的主題,並設置一下屬性:
values-v21/themes.xml
<style name="AppTheme">
<item name="android:">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
這里解釋一下:
,將它設置為true,系統將在你的window裡面繪制status
bar,默認為 TRUE
,之所以要寫出來是因為你的theme有可能是繼承過來的,確保為true。(在這里小插曲一下,因調試時,總以為注釋了這段代碼就以為是false,程
序員思維害苦了我。另外從命名來看,Android把它稱為system bar,可能是為了與能被我們處理的status bar區分開而做的改變。)
statusBarColor 設置為透明是因為我們不再需要系統的status bar,因為我們無法控制它的位置,後面我們將交由 DrawerLayout 來處理。
使用DrawerLayout
首先,你的布局文件應該是和這個類似的:
<android.support.v4.widget.DrawerLayout
xmlns:android="url"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- The rest of your content view -->
</LinearLayout>
<!-- The navigation drawer -->
<ScrimInsetsFrameLayout xmlns:android="rul"
xmlns:app="url"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
<!-- Your drawer content -->
</ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
在這裡布局裡面我們用到了一個的開源類 ScrimInsetsFrameLayout ,它的主要作用就是利用 fitsSystemWindows
的回調方法 fitSystemWindows(Rect insets) 來獲取status bar的大小,然後調整畫布已達到去掉status
bar的效果,所以我們需要在ScrimInsetsFrameLayout 下設置 fitsSystemWindows
為true。當然你也可以不使用這個類,而改用 layout_marginTop 屬性來達到效果。
insetForeground 這個屬性是ScrimInsetsFrameLayout自帶的,表示插入區域的前景色,我們設置為帶透明的黑色#4000。別忘了使用這個屬性需要添加如下代碼到attrs.xml里:
values/attrs.xml
<declare-styleable name="ScrimInsetsView">
<attr name="insetForeground" format="reference|color" />
</declare-styleable>
自此,我們已經實現了將DrawerLayout抽屜的那一部分顯示在 Toolbar 和systembar(為了和下面的status
bar區分,我們稱為system bar)之間了,可是system bar的顏色被我們設置了透明,所以我們接下來要改變status
bar的顏色。
改變Status bar的顏色
你可能已經注意到剛才的布局裡面 DrawerLayout 的 fitsSystemWindows 屬性設置了為true,這是因為我們要在代碼裡面使用了 DrawerLayout 設置status bar顏色的方法:
// 在這里我們獲取了主題暗色,並設置了status bar的顏色
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int color = typedValue.data;
// 注意setStatusBarBackgroundColor方法需要你將fitsSystemWindows設置為true才會生效
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
drawerLayout.setStatusBarBackgroundColor(color);
使用ToolBar來代替ActionBar
在代碼裡面這樣設置:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);