3

Android應用程序目前支持基於定位,屏幕大小,晝夜等Android的佈局資源

但是不同的佈局資源,我想提供針對視力不好的用戶使用的佈局,例如使用帶黃色背景和黑色文本的佈局。

我錯過了Android已經支持的東西嗎?

我可以實現自定義res/layout-WAI或res/layout-DDA文件夾嗎?

回答

1

您無法創建自定義配置限定符。 當前支持的限定符列於here

我會建議採取以下解決辦法():

  1. 每個現有佈局創建WAI一個特殊的佈局,具有相同的名稱,但後綴「_wai」

    example_layout.xml 
    example_layout_wai.xml 
    
  2. 根據系統需求創建一種方法來解決相應的佈局。假設我們有一個方法isWAI(),解決方法看起來是這樣的:

    public int resolveLayoutResourceID(int layoutResID) { 
        int newLayoutResID = layoutResID; 
        if (isWAI()) { 
         String layoutResName = getResources().getResourceEntryName(layoutResID); 
         String newLayoutResName = layoutResName + "_wai"; 
         newLayoutResID = getResources().getIdentifier(newLayoutResName, "layout", getPackageName()); 
        } 
        return newLayoutResID; 
    } 
    
  3. 創建BaseActivity類所有類(或使用工具靜態函數),這將覆蓋setContentView方法。在那裏你將添加一個邏輯來選擇佈局。

    @Override 
    public void setContentView(int layoutResID) { 
        int newLayoutResID = resolveLayoutResourceID(layoutResID) 
        super.setContentView(newLayoutResID); 
    } 
    
+0

我喜歡這種方法。它是一個簡單/聰明的解決方案,可以理解。感謝您抽出寶貴時間回覆 – Hector

1

顏色(黃色背景和黑色文字),字體和字體大小爲Styles and Themes的話題。

除非視覺imapaired人需要自己的佈局(安排,GUI元素的順序)可以實現與a style chooser的設置,可以提供兩個不同的佈局被應用到每一個佈局

+0

這是真的,但如果我可以提供WAI佈局,並且這些佈局會在用戶啓用其他任何輔助功能時自動提供給用戶,則會更容易實現。例如三倍放大或回聲等。編程式更改樣式,主題和字體要求我的開發人員編寫更多需要測試和維護的代碼行,而不是簡單地提供替代佈局。我試圖讓整體的觀點是...如果Android提供了一個白天和黑夜的替代佈局資源,爲什麼不能實現無障礙佈局? – Hector

1

相反,你可以參數化的意見單一佈局。因此,意見的佈局將採取參數(例如背景顏色,文本顏色)從它們在膨脹的背景主題

所以,這就是我們要達到:

<android.support.constraint.ConstraintLayout 
    android:background="?attr/bgColor" 
    ... > 

    <TextView 
     android:textColor="?attr/textColor" 
     ... /> 

</android.support.constraint.ConstraintLayout> 

?attr/someAttribute會從當前背景的主題出發。

values/創建於attrs.xml屬性:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <declare-styleable name="MyAttrs"> 
     <attr name="bgColor" format="reference|color"/> 
     <attr name="textColor" format="color"/> 
    </declare-styleable> 

</resources> 

styles.xml宣佈從一個共同的主題延伸出的兩個主題:

<resources> 

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     ... 
    </style> 

    <style name="AppTheme.Default"> 
     <item name="bgColor">@color/red</item> 
     <item name="textColor">@color/blue</item> 
    </style> 

    <style name="AppTheme.Accessibility"> 
     <item name="bgColor">@color/orange</item> 
     <item name="textColor">@color/yellow</item> 
    </style> 

</resources> 

然後,在您的活動進行斷言,並設置一個正確的主題:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    setTheme(isAccessibility ? R.style.AppTheme_Accessibility : R.style.AppTheme_Default); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 
    ... 
} 

或者,如果您必須在運行時執行此操作,則可以使用ContextThemeWrapper以適當的主題膨脹特定的視圖。

Context wrapper = new ContextThemeWrapper(MyFragment.this.getContext(), R.style.AppTheme_Accessibility); 
// inflating with a `wrapper`, not with the activity's theme 
View themedView = View.inflate(wrapper, R.layout.some_layout, parent); 

這是更好的方法則提供兩個單獨的佈局,因爲它事不聞不問你維護兩個佈局時,在UI的改變發生。

+0

我很欣賞你的方法。然而主題和風格總是讓我感到悲傷。我收費的佈局方法更清晰...並通過使用包括我可以減少多重佈局問題。 – Hector