導航:首頁 > 文字圖片 > 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文字圖片相關的資料

熱點內容
耐克女鞋圖片與價格 瀏覽:678
在微信怎麼發文字圖片不顯示圖片 瀏覽:310
女生唯美素描圖片大全 瀏覽:653
犀利眼睛男生圖片 瀏覽:273
土豆長出來的圖片大全 瀏覽:553
紅色眼睛頭像圖片男生 瀏覽:732
明星q版動漫圖片大全 瀏覽:563
好看的波波頭發型圖片 瀏覽:696
賣水果圖片加文字 瀏覽:70
15米實木床圖片價格 瀏覽:403
word附件圖片錯位 瀏覽:570
鍋蓋頭發型女圖片大全 瀏覽:996
綠馳電動車價格圖片 瀏覽:480
簡單包發發型圖片分解 瀏覽:966
張雲雷光頭高清圖片 瀏覽:96
金屬亮色衣服圖片 瀏覽:603
衣服的頭像卡通圖片 瀏覽:119
衣服給錢就賣廣告圖片大全 瀏覽:139
攝影圖片如何顯示位置 瀏覽:507
比基尼美女海灘圖片 瀏覽:560