9

當使用基於xml的佈局使用AppCompat 22.1.0時,並非所有支持的小部件都使用Android 4.4爲我的碎片着色或材質化。AppCompat v22.1.0不會將所有的xml小部件正確地轉換爲片段

我看到此行爲具有以下部件(其他未測試):

  • 單選按鈕(無色彩的顏色)
  • 複選框(無色彩的顏色)
  • 微調(設備默認的主題應用)
  • 的EditText(設備默認的主題應用)
  • 的RatingBar(設備默認的主題應用)
  • 按鈕(設備defaul t主題)

它曾經在AppCompat v22.0.0中工作。

截圖(左4.4,右5.0):

Example screenshot

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()) 
        .commit(); 
     } 
    } 

    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 
} 

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="RadioButton test"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="CheckBox test"/> 

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/someStrings"/> 
</LinearLayout> 

的themes.xml

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

用22.1.1固定 –

+0

確實! Google快速回復! –

回答

11

這是目前報告爲錯誤:https://code.google.com/p/android/issues/detail?id=169760

暫時的解決辦法是使用片段父活動LayoutInflater:getActivity().getLayoutInflater()代替供給LayoutInflater在onCreateView方法。

例子:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_main, container, false); 
    return rootView; 
} 

注:另一種方法是使用特殊的程序兼容性部件在你的XML佈局:

  • android.support.v7.widget.AppCompatRadioButton
  • android.support.v7.widget.AppCompatCheckBox
  • andro id.support.v7.widget.AppCompatSpinner

但是這基本上意味着您需要用AppCompat替換每個小部件。

+0

哎。很高興仍然在v22上。 – natario

+0

@Rolf你可以標記這是正確的,如果你知道了嗎? –

+0

優秀的解決方法。 – Matthew

2

您可以強制應用於享有主題,只需要在父視圖中添加這些行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      tools:context="com.example.yourActivityThatHasATheme" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
</LinearLayout> 

然後線性佈局內的所有意見採取宣佈在各自的活動中強調色清單(通過主題)。

相關問題