![](/img/trans.png)
[英]Android button styling looks different on different devices (AOSP and Samsung Galaxy)
[英]button looks different on different android devices or versions
我有兩個簡單的按鈕,想給他們一個自定義的外觀,所以我只是給按鈕提供了背景色。 這在運行4.2的Htc One上看起來不錯,但是當我在舊的Lg P925上測試它時,按鈕的高度剛好包裹了它的內容(就像默認的填充消失了一樣)。
像這樣:
因此,我做了一些進一步的研究,發現我必須使用狀態列表繪制功能才能向按鈕添加功能。
所以我這樣做是這樣的:
Button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_focused" />
<item android:drawable="@drawable/button_normal" />
</selector>
按鈕集中
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/greybutton"/>
<corners android:radius="0dp" />
<stroke android:color="@color/greybutton" android:width="2dp" />
<padding android:top="5dp" android:bottom="5dp"
android:left="10dp"android:right="10dp" />
</shape>
按下按鈕
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/greybutton"/>
<corners android:radius="0dp" />
<stroke android:color="@color/greybutton" android:width="2dp" />
<padding android:top="5dp" android:bottom="5dp" android:left="10dp"
android:right="10dp" />
</shape>
按鍵正常
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFFFF" android:endColor="#A4A4A4"
android:angle="270" />
<solid android:color="@color/darkerGrey"/>
<corners android:radius="0dp" />
<padding android:top="5dp" android:bottom="5dp" android:left="10dp"
android:right="10dp"/>
</shape>
樣式
<style name="button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button</item>
</style>
然后我將樣式添加到按鈕:
<Button
android:id="@+id/SignUpBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/CreateAcct"
android:textColor="@color/white"
style="@style/button"
android:textSize="17sp"
/>
這就是我得到的:
在我的HTC上(運行4.2)
這里的按鈕更大,因為它在按鈕的默認系統填充中添加了5dp頂部和底部填充(來自我的state drawables xml)。
在運行於2.3的Lg P925上,我得到以下信息:
按鈕比我的htc上的按鈕小。 看來,當我向按鈕添加背景時,它將擺脫默認的填充。 我只是不知道
如何創建在所有設備上看起來都一樣的按鈕?
*文本字體因設備而異,因此請使用字體類設置按鈕的字體*
像這樣
在Assets文件夾中創建文件夾字體,然后粘貼abc.ttf文件。 基本上.ttf文件是包含免費字體的文件 。
Button txt = (Button) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "abc.TTF");
txt.setTypeface(font);
或者你可以使用這樣的使用Android的預定義字體
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:typeface="sans"/>
sans,monospace,serif是預定義的字體
享受編碼
它會工作
我相信關鍵是這樣的聲明:
<style name="button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button</item>
</style>
請注意,您正在擴展父樣式,該樣式可能會因Android版本等而異。請在沒有父樣式的情況下進行嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.