Mã sau đây là để hiển thị nút tùy chỉnh trong Android. Thật không may, tất cả các giá trị đệm ở những vị trí khác nhau đều ảnh hưởng đến cách nút được hiển thị chỉ dành cho Android 2.3, nhưng trên Android 4.X nó không có hiệu lực, tức là phần đệm luôn giống nhau, bất kể bạn đặt gì ở đây. Tại sao?Nút tùy chỉnh với nền có thể kéo: Sự khác biệt về đệm trong Android 4 so với Android 2.3
Cách bố trí XML:
<Button
android:id="@+id/sample_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_caption"
android:padding="6dp"
android:layout_marginBottom="6dp"
android:layout_marginRight="6dp"
style="@style/CustomButton"
android:textSize="16sp" />
Phong cách:
<style name="CustomButton">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:padding">6dp</item>
<item name="android:background">@drawable/custom_button</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
</style>
Các drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid
android:color="#60ff0000" />
<stroke
android:width="1dp"
android:color="#ffffff" />
<corners
android:radius="4dp" />
<padding
android:left="6dp"
android:top="6dp"
android:right="6dp"
android:bottom="6dp" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#90ff0000" />
<stroke
android:width="1dp"
android:color="#ffffff" />
<corners
android:radius="4dp" />
<padding
android:left="6dp"
android:top="6dp"
android:right="6dp"
android:bottom="6dp" />
</shape>
</item>
</selector>
Khi tôi đặt tất cả các thes giá trị padding cho một giá trị nhỏ như 2, nút được hiển thị gần như không có đệm trên Android 2.3.3 (có thể đúng) nhưng với một padding vẫn còn rất lớn trên Android 4.X (có thể là sai). Ai đó có thể thấy lý do tại sao?
Vui lòng trợ giúp - đạo đức của câu chuyện này là luôn kiểm tra kiểu/mã trên Github để tìm manh mối về hành vi mặc định. –
Có, nhận được điều đó;) – caw