導航:首頁 > 文字圖片 > androidbutton文字圖片

androidbutton文字圖片

發布時間:2022-02-11 13:15:03

A. 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背景圖片。

B. 怎麼將Button上的文字和android:drawableLeft都居中

Button上的android:drawableLeft設置的圖片就是居左,無法和文字一起居中,文字屬性可以通過android:layout_gravity設置居中,想要android:drawableLeft設置的圖片居中,可以通過另外的方法來實現,建議:兩張圖片合二為一,android:drawableLeft設置的圖片直接和按鈕背景圖合到一張圖片上,android:drawableLeft設置的圖片大約位於按鈕背景圖三分之一處(黃金分割點),右側空出文本區域。

C. Android Button設置.9格式圖片當背景,發現Button的text不顯示了

我覺得這個是你畫的 .9 圖的問題

右邊跟下邊有畫成這樣才能顯示文字內容,至於為什麼,請自行搜索一下.9圖的用法

D. android圓形灰色圖片做按鈕背景,圓形背景上需要疊加一張圖片和文字,文字要在圖片的下方,怎麼實現

用android:drawableTop設置圖片就好了。

E. 怎麼設置android button 里文字和drawableleft 居左

將圖標和text在button的中間位置,現在是文字和圖標距離很遠
[專業]答案:Button上的android:drawableLeft設置的圖片就是居左,無法和文字一起居中,文字屬性可以通過android:layout_gravity設置居中,想要android:drawableLeft設置

F. android中button上的圖案可以自定義嗎

可以的。
package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;

public class ImageTextButton2 extends Button {

private int resourceId = 0;
private Bitmap bitmap;

public ImageTextButton2(Context context) {
super(context,null);
}

public ImageTextButton2(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
this.setClickable(true);
resourceId = R.drawable.icon;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
}

public void setIcon(int resourceId)
{
this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

// 圖片頂部居中顯示
int x = (this.getMeasuredWidth() - bitmap.getWidth())/2;
int y = 0;
canvas.drawBitmap(bitmap, x, y, null);
// 坐標需要轉換,因為默認情況下Button中的文字居中顯示
// 這里需要讓文字在底部顯示
canvas.translate(0,(this.getMeasuredHeight()/2) - (int) this.getTextSize());
super.onDraw(canvas);
}

}

G. android點擊按鈕之後改變按鈕的文字

想在android點擊按鈕之後改變按鈕的文字需要給按鈕添加一個監聽事件,然後一監聽到該事件再給這個按鈕調用setText()方法,在方法里可以給按鈕設置文字,具體操作如下:

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

H. android 如何像win8圖標一樣在圖片按鈕上加文字

可以不使用button控制項,而是用布局 ,在布局中定義文字大小位置,而布局大小就是你所謂的按鈕大小其背景色就用此圖片,最終給這個布局添加上onclick事件就可以了

I. android 點擊ImagButton切換文字和圖片用什麼來實現

用xml來實現,再寫一個效果描述的xml來指定正常狀況下是什麼圖片,按下是什麼圖片等都可以

J. 如何在ImageButton上寫字Android

因為ImageButton本身沒有text屬性,所以需要自定義一個新的ImageButton類,屬於自定義View。安卓里所有的UI界面都是由View類和ViewGroup類及其子類組合而成的。ps:找美工在背景圖片上加文字最方便。

閱讀全文

與androidbutton文字圖片相關的資料

熱點內容
手機上怎麼把圖片合成word 瀏覽:330
ps如何放大或縮小圖片 瀏覽:484
特種兵照片圖片大全 瀏覽:797
帶文字的男生圖片 瀏覽:141
張繼科穿馬龍衣服圖片 瀏覽:81
服裝設計發型圖片 瀏覽:610
怎麼做文字圖片軟體 瀏覽:322
女孩頭頂鳳凰旋圖片 瀏覽:614
將文字轉換成圖片 瀏覽:642
古代女孩國畫圖片 瀏覽:392
香草衣服丟河裡圖片 瀏覽:910
場落地地毯女孩圖片 瀏覽:600
圖片15歲女孩 瀏覽:991
word能壓縮圖片大小 瀏覽:291
女生勵志圖片帶字唯美清晰 瀏覽:623
小寫英文字母圖片 瀏覽:795
紅色衣服卷發叼支煙的頭像圖片 瀏覽:805
小清新簡單插畫圖片 瀏覽:385
大姐姐女孩兒游泳的圖片 瀏覽:707
未來在哪裡圖片大全 瀏覽:804