tintandroid
A. 在android中怎樣讓按鈕漂浮在圖片上
android懸浮按鈕(Floating action button)的兩種實現方法
最近android中有很多新的設計規范被引入,最流行的莫過於被稱作Promoted Actions的設計了,Promoted Actions是指一種操作按鈕,它不是放在actionbar中,而是直接在可見的UI布局中(當然這里的UI指的是setContentView所管轄的范圍)。因此它更容易在代碼中被獲取到(試想如果你要在actionbar中獲取一個菜單按鈕是不是很難?),Promoted Actions往往主要用於一個界面的主要操作,比如在email的郵件列表界面,promoted action可以用於接受一個新郵件。promoted action在外觀上其實就是一個懸浮按鈕,更常見的是漂浮在界面上的圓形按鈕,一般我直接將promoted action稱作懸浮按鈕,英文名稱Float Action Button簡稱(FAB,不是FBI哈)。
floatactionbutton是android l中的產物,但是我們也可以在更早的版本中實現。假設我這里有一個列表界面,我想使用floatactionbutton代表添加新元素的功能,界面如下:
要實現floatactionbutton可以有多種方法,一種只適合android L,另外一種適合任意版本。
用ImageButton實現
這種方式其實是在ImageButton的屬性中使用了android L才有的一些特性:
<ImageButton
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/plus"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:tint="@android:color/white"
android:id="@+id/fab"
android:elevation="1dp"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/fab_anim"
/>
仔細一點,你會發現我們將這個ImageButton放到了布局的右下角,為了實現floatactionbutton應該具備的效果,需要考慮以下幾個方面:
·Background
·Shadow
·Animation
背景上我們使用ripple drawable來增強吸引力。注意上面的xml代碼中我們將background設置成了@drawable/ripple,ripple drawable的定義如下:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
既然是懸浮按鈕,那就需要強調維度上面的感覺,當按鈕被按下的時候,按鈕的陰影需要擴大,並且這個過程是漸變的,我們使用屬性動畫去改變translatioz。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:ration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/start_z"
android:valueTo="@dimen/end_z"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:ration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/end_z"
android:valueTo="@dimen/start_z"
android:valueType="floatType" />
</item>
</selector>
使用自定義控制項的方式實現懸浮按鈕
這種方式不依賴於android L,而是碼代碼。
首先定義一個這樣的類:
public class CustomFAB extends ImageButton {
...
}
然後是讀取一些自定義的屬性(假設你了解styleable的用法)
private void init(AttributeSet attrSet) {
Resources.Theme theme = ctx.getTheme();
TypedArray arr = theme.obtainStyledAttributes(attrSet, R.styleable.FAB, 0, 0);
try {
setBgColor(arr.getColor(R.styleable.FAB_bg_color, Color.BLUE));
setBgColorPressed(arr.getColor(R.styleable.FAB_bg_color_pressed, Color.GRAY));
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] {android.R.attr.state_pressed}, createButton(bgColorPressed));
sld.addState(new int[] {}, createButton(bgColor));
setBackground(sld);
}
catch(Throwable t) {}
finally {
arr.recycle();
}
}
在xml中我們需要加入如下代碼,一般是在attr.xml文件中。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FAB">
<!-- Background color -->
<attr name="bg_color" format="color|reference"/>
<attr name="bg_color_pressed" format="color|reference"/>
</declare-styleable>
</resources>
使用StateListDrawable來實現不同狀態下的背景
private Drawable createButton(int color) {
OvalShape oShape = new OvalShape();
ShapeDrawable sd = new ShapeDrawable(oShape);
setWillNotDraw(false);
sd.getPaint().setColor(color);
OvalShape oShape1 = new OvalShape();
ShapeDrawable sd1 = new ShapeDrawable(oShape);
sd1.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0,0,0, height,
new int[] {
Color.WHITE,
Color.GRAY,
Color.DKGRAY,
Color.BLACK
}, null, Shader.TileMode.REPEAT);
return lg;
}
});
LayerDrawable ld = new LayerDrawable(new Drawable[] { sd1, sd });
ld.setLayerInset(0, 5, 5, 0, 0);
ld.setLayerInset(1, 0, 0, 5, 5);
return ld;
}
最後將控制項放xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.survivingwithandroid.fab"
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"
tools:context=".MyActivity">
...
<com.survivingwithandroid.fab.CustomFAB
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
custom:bg_color="@color/light_blue"
android:tint="@android:color/white"
/>
</RelativeLayout>
B. android studio為什麼創建的時候選擇的API最低版本是19,最後都變成23
剛剛注意到你的問題,感覺之前的回答都沒有答到點上,不知道現在回答對你是否還有幫助。
出現這種問題,可能是由於build.gradle(app)中minSdkVersion被手動改過。但是我認為更有可能是你在android studio編輯時使用了高api代碼(如tint等等)並用燈泡或alt+enter推薦修改中的Add requires API() annotation來處理call requires API level 23(Current min 19)之類的錯誤。
解決方法:在這些位置使用if判斷環繞並且在else中給出低api的處理方法。
C. android studio怎麼做沉浸式狀態欄
答:沉浸式通知欄Android4.4以上才支持的新特性。4.3不支持。 具體實現方式如下: 1.新建個公共style,設置android:fitsSystemWindows=true true 2. 修改AndroidManifest.xml,讓所有的activity樣式默認設置為AppBaseTheme(*不同項目要靈活處理,筆...
D. 安卓開發,圖標背景顏色怎麼更換
android應用圖標背景的替換方法為:
1、:項目下面—res文件夾—drawable-hdpi/drawable-ldpi/drawable-mdpi等文件夾下就是圖標(解析度不同所以幾個文件都是hdpi是高解析度,ldpi是中等解析度,mdpi是低解析度,項目建立的時候有選擇)
2、然後在Manifest文件裡面,指定的icon就是
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher" //這個就是指定icon的代碼,不需要修改或者改成想要的文件的名字,記住圖片的後綴為png
android:label="@string/app_name"
即可。