android自定義button樣式
① android 中如何自定義dialog里button的背景顏色
選定Alterdialog所關聯的layout文件即可
就像一般的頁面是
setLayoutXXX什麼的
忘記了
② android 怎麼動態設置button 的style
自定義樣式方法,可以直接通過定義xml文件來實現不同的樣式:
只需要修改button_style文件,同樣三種狀態分開定義:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient android:startColor="#000000" android:endColor="#ffffff"
android:angle="180" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="5dip" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
gradient 主體漸變 startColor開始顏色,endColor結束顏色 ,angle開始漸變的角度(值只能為90的倍數,0時為左到右漸變,90時為下到上漸變,依次逆時針類推)
stroke 邊框 width 邊框寬度,color 邊框顏色
corners 圓角 radius 半徑,0為直角
padding text值的相對位置
③ 如何自定義RadioButton樣式
我們知道Android控制項里的button,listview可以用xml的樣式自定義成自己希望的漂亮樣式。
最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable為自己定義的.xml文件(res/drawable文件下),但是不成功,到網上查找,也沒有正確的說法,我就開始自己嘗試,最後做好了。
其實方法很簡單,同樣在res/drawable新建radiobutton.xml如下
01 <selector xmlns:android="http://schemas.android.com/apk/res/android">
02 <item
03 android:state_enabled="true"
04 android:state_checked="true"
05 android:drawable="@drawable/check" />
06 <item
07 android:state_enabled="true"
08 android:state_checked="false"
09 android:drawable="@drawable/checknull" />
10 </selector>
check和checknull分別為選中和位選中的圖片。
然後在你的布局文件中,RadioButton 布局
設置android:button = "@drawable/radiobutton",就可以了!
④ 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中字體的格式
使用方法 第一種是在listview中配置android:listSelector=」@drawable/list_item_bg」 第二種是在listview的item中添加屬性android:background=」@drawable/list_item_bg」 第三種是java代碼中使用: Drawable drawable = getResources()/apk/res/android">" target="_blank">schemas/apk/res/android"></a> <item android:state_selected="true" android:color="#FF0000" /> <item android:state_focused="true" android:color="#00FF00" /> <item android:state_pressed="true" android:color="#0000FF" /> <item android:color="#000000" /></selector>使用方法 android:textColor="@drawable/button_color" 更復雜的效果 還可以實現更復雜的效果,例如漸變等等。 drawable/button_color/apk/res/android">" target="_blank">schemas/apk/res/android"></a> <item android:state_pressed="true"> <!-- 定義當button 處於pressed 狀態時的形態。--> <shape> <gradient android:startColor="#8600ff" /> <stroke android:width="2dp" android:color="#000000" /> <corners android:radius="5dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape> </item> <item android:state_focused="true"> <!-- 定義當button獲得 focus時的形態 --> <shape> <gradient android:startColor="#eac100"/> <stroke android:width="2dp" android:color="#333333" color="#ffffff"/> <corners android:radius="8dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape> </item></selector>使用方法 android:background="@drawable/button_color" android:focusable="true"
⑥ android button 樣式怎麼寫
如何自定義android Button樣式
android自帶的樣式比較難看,如何能夠自定義按鈕的樣式,使其顯示的跟美工設計的效果一樣,現與大家分享下
工具/原料
eclipse ADT
方法/步驟
在layout中添加2個按鈕,從下圖中可以看出在按鈕中調用了style和android:background屬性,這兩個屬性一個是自定義樣式,一個是給按鈕添加背景圖片,下面詳細介紹下
展開res目錄,可以看到在values目錄下有styles.xml文件,該文件用於自定義樣式,雙擊打開
下圖中標注的是我自定義的樣式,name為BtnStyle,當按鈕調用自定義樣式的時候訪問這個name
下圖是在button中調用自定義樣式的方法,比較簡單
下面分享下如何往按鈕中添加自定義圖片,使按鈕看起來更漂亮些,因不同手機解析度不同,那必然牽扯到圖片的拉伸,在android系統下有個很好的技術「九宮格「,可以對圖片進行處理,只對局部進行拉伸,給工具目錄存儲在android\sdk\tools\draw9patch.bat,經過該工具處理的圖片以.9.png結尾,放到drawable文件夾中
下圖是在Button中通過android:background屬性載入圖片的方法,至此我們自定義的按鈕樣式也就完成了,當然這只是個引子,在具體的項目工程中實現的效果要比這個demo復雜很多,有好的設計思路歡迎交流。
出自:http://jingyan..com/article/454316ab4bdc66f7a7c03a13.html
⑦ android studio中的button有哪些方法
先介紹下修改原理:首先打開位於android.widget包下面的Button.java文件,這里有一句關鍵的代碼如下:
public
Button(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
com.android.internal.R.attr.buttonStyle);
}
1
2
3
其中com.android.internal.R.attr.buttonStyle就是我們修改樣式的關鍵了,網上的教程的修改方法大都是:
<Button
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:text="價格"
/>
1
2
3
4
5
6
也就是在對應的xml裡面button控制項裡面編寫style達到目的。
但是如果我們的app需要完全統一整個應用的button的樣式,那麼就需要在每一個button裡面添加style。
這顯然效率太低下了。
接下來打開我們項目中values文件夾下面的styles.xml文件,我們創建安卓項目的時候,會有一個默認的styles文件。
打開之後找到這段代碼:
<style
name="AppBaseTheme"
parent="Theme.Holo.Light">
<!--
Theme
customizations
available
in
newer
API
levels
can
go
in
res/values-vXX/styles.xml,
while
customizations
related
to
backward-compatibility
can
go
here.
-->
</style>
<!--
Application
theme.
-->
<style
name="AppTheme"
parent="AppBaseTheme">
1
2
3
4
5
6
7
8
9
10
不保證讀者的默認styles.xml和我的是一樣的,不過大概是這個樣子,有可能讀者的最低支持是2.3、那麼就沒有Them.Light。
我們使用eclipse的快捷鍵打開這個Theme.Holo.Light。可以看到如下代碼:
<style
name="Theme.Holo.Light"
parent="Theme.Light">
<item
name="colorForeground">@android:color/bright_foreground_holo_light</item>
<item
name="colorForegroundInverse">@android:color/bright_foreground_inverse_holo_light</item>
<item
name="colorBackground">@android:color/background_holo_light</item>
<item
name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_holo_light</item>
<item
name="disabledAlpha">0.5</item>
<item
name="backgroundDimAmount">0.6</item>
⑧ 安卓UI自定義radiobutton樣式
自定義RadioButton樣式分為兩步:
1、自定義好樣式:
打開style.xml,添加一個item
<stylename="RadiobuttonStyle">
<itemname="android:gravity">center</item>
<itemname="android:textSize">16sp</item>
<itemname="android:textColor">@color/text_select_color</item>
<itemname="android:button">@null</item>
<itemname="android:drawableTop">@drawable/select_rbtn_home</item>
</style>
2、在RadioButon中引用:
<RadioButton
android:id="@+id/tv_tab_home"
style="@style/RadiobuttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/tab_home"/>
⑨ android開發 設置自定義按鈕為半透明
半透明<Button android:background="#e0000000" ... />
透明<Button android:background="#00000000" ... />
顏 色和不透明度 (alpha) 值以十六進製表示法表示。任何一種顏色的值范圍都是 0 到 255(00 到 ff)。對於 alpha,00 表示完全透明,ff 表示完全不透明。表達式順序是「aabbggrr」,其中「aa=alpha」(00 到 ff);「bb=blue」(00 到 ff);「gg=green」(00 到 ff);「rr=red」(00 到 ff)。例如,如果您希望對某疊加層應用不透明度為 50% 的藍色,則應指定以下值:7fff0000
設置背景圖片透明度(超簡單)
Java代碼
View v = findViewById(R.id.content);//找到你要設透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值
⑩ 怎麼Android編程中設置Button的字體顏色呢
<Button
android:text="123"
android:textColor="@color/aliceblue" //設置字體顏色
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
望採納!