當前位置:首頁 » 安卓系統 » android設置button圖片

android設置button圖片

發布時間: 2022-04-26 06:45:14

『壹』 Android RadioButton控制項怎麼設置圖片(屬性)android:(drawable)

我覺得RadioButton原始的button如果設置為 android:button="@null「 而用android:drowableLeft=」@drawable/radiobtn_selector「;這樣顯示圖片的話效果會更加好,而且可以用android:drawablePading=」10dp「這個屬性控制圖片和字兒的距離。 希望可以幫到你。Thanks

『貳』 如何自定義android Button樣式

在windows7操作系統Android studio中按照如下方法定義button的樣式。

1、首先使用Android studio創建一個項目,項目結構如下:

『叄』 android中設置Button點擊切換按鈕的背景圖片,點第一次換了一張背景,怎麼設置再次換張背景

在onclick事件下 設置按鈕的背景圖片,但是要隨機,把你想要顯示的圖片放到數組裡面,然後隨機下角標,選出來的圖片作為背景圖片就可以了,每次點擊就會生成一個圖片,想要不重復,那就寫個去重復的方法就行了。沒理解的話追問把

『肆』 android,Button設置圖片,寬和高都設置wrap_content,但是設置的圖片還是有背景溢出

圖片設在背景里,android:background="@drawable/ys",這樣就沒問題了。

『伍』 在android中如何實現以下效果,即在圖片上設置button,並且可以控制button的位置

他這個是在你寫的根layou設置背景為一張圖片,然後添加button控制項就是了,可以用RelativeLayout,然後對button控制項添加屬性android:alignParentBottom="true"他就顯示在底部了,其他的都差不多

『陸』 android 按鈕設置點擊更換imagebutton背景

button.setBackgroundResource(R.drawable.beijing1);
上面是改變按鈕背景的代碼
可以做兩組圖片,分別為button1的選中和為選擇狀態、button2的選中和為選擇狀態,讓後再button1和button2的點擊事件中,對兩個按鈕的背景進行改變,就可以了

『柒』 在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>

『捌』 android中button上設置圖片

android中button上設置圖片的方法為:
1、自定義MyButton類
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}

2、 在xml布局文件中添加MyButton控制項,像應用普通的Button控制項一樣。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定義的MyButton類所在的包名
3、在onCreate()中載入MyButton控制項。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示為按下btn時背景圖片,btn_u為默認狀態下btn背景圖片。

『玖』 android 編程怎樣在圖片里添加Button按鈕。

外面用布局
設置布局的背景為圖片
然後布局裡面添加一個button

『拾』 Android 怎麼讓圖片顯示在button中間

使用ImageButton就可以實現,代碼如下:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"//將控制項中的圖片屬性定義為居中顯示
android:src="@drawable/xxx" />

下面是scaleType的幾種屬性的含義:
ImageView.ScaleType.CENTER|android:scaleType="center" 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
ImageView.ScaleType.CENTER_CROP|android:scaleType="centerCrop" 按比例擴大圖片的size居中顯示,使得圖片長 (寬)等於或大於View的長(寬)
ImageView.ScaleType.CENTER_INSIDE|android:scaleType="centerInside" 將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長(寬)等於或小於View的長(寬)
ImageView.ScaleType.FIT_CENTER|android:scaleType="fitCenter" 把圖片按比例擴大(縮小)到View的寬度,居中顯示
ImageView.ScaleType.FIT_END|android:scaleType="fitEnd" 把圖片按比例擴大(縮小)到View的寬度,顯示在View的下部分位置
ImageView.ScaleType.FIT_START|android:scaleType="fitStart" 把圖片按比例擴大(縮小)到View的寬度,顯示在View的上部分位置
ImageView.ScaleType.FIT_XY|android:scaleType="fitXY" 把圖片按照指定的大小在View中顯示
ImageView.ScaleType.MATRIX|android:scaleType="matrix" 用matrix來繪制

熱點內容
安卓加速器怎麼關 發布:2025-05-15 11:38:16 瀏覽:463
密碼鎖壞了如何打開 發布:2025-05-15 11:30:19 瀏覽:837
怎樣增加共享文件夾連接數量 發布:2025-05-15 11:24:50 瀏覽:961
安卓如何關閉單應用音量 發布:2025-05-15 11:22:31 瀏覽:351
抖音電腦後台伺服器中斷 發布:2025-05-15 11:11:59 瀏覽:307
sql2008伺服器 發布:2025-05-15 11:03:27 瀏覽:306
我的世界pe伺服器創造 發布:2025-05-15 10:51:17 瀏覽:608
移動端打吃雞要什麼配置 發布:2025-05-15 10:48:16 瀏覽:756
我的世界哪五個伺服器被炸了 發布:2025-05-15 10:36:16 瀏覽:994
ehcache存儲對象 發布:2025-05-15 10:35:31 瀏覽:528