[英]How to scale a custom button drawable for different screen sizes in android studio?
我使用以下xml文件創建了一個可繪制的自定義按鈕:
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/button_pressed" />
<item
android:state_enabled="true"
android:drawable="@drawable/button_enabled"/>
</selector>
button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size
android:width="70dp"
android:height="70dp"/>
<gradient
android:startColor="#000000"
android:centerColor="#000000"
android:endColor="#000000"
android:angle="90"/>
<padding android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<stroke
android:width="3dp"
android:color="#05B402" />
</shape>
button_enabled.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size
android:width="70dp"
android:height="70dp" />
<gradient
android:startColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="90"/>
<padding android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<stroke
android:width="3dp"
android:color="#05B402" />
</shape>
我之所以這樣,是因為我使用如下代碼將此可繪制對象應用於按鈕:
Button b = new Button(getActivity());
b.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.button));
但是,我的問題出現在使用標簽中。 自然,這會使按鈕在不同屏幕上的大小相同。 有沒有一種方法可以縮放此可繪制對象,以便在不同的屏幕上使用不同的大小?
我知道,最簡單的方法是為不同的屏幕尺寸創建多個xml文件。
去做這個,
在res
文件夾中,右鍵單擊drawable
文件夾。
它應該顯示一種形式:
將名稱設置為與其他名稱完全相同
但添加一個准輸入:
這將為該特定大小創建一個新的文件夾和xml文件。
對這些文件夾中的每個xml文件進行更改,以獲取正確的縮放比例
編輯:
從這里找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.