AppCompat Vector drawable在RadioButton选择器上崩溃

本文关键字:选择器 崩溃 RadioButton Vector drawable AppCompat | 更新日期: 2023-09-27 18:14:44

在选择器中尝试设置形状,其他都可以。当我改变到<vector> -应用程序崩溃。我可以设置为ImageViev app:srcCompat="@drawable/icon和所有ok,但如果我设置为RadioButton app:srcCompat="@drawable/icon_selection和用于项目

`<item android:drawable="@drawable/icon_active" android:state_checked="true"/>
<item android:drawable="@drawable/icon" android:state_checked="false"/>`

我看到API19的错误,但在API21上一切正常!

崩溃代码:

`Android.Views.InflateException: Binary XML file line #1: 
Error inflating class android.widget.ToggleButton`

我使用23.4.0.1库。

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/a2"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:windowNoTitle="true"
    android:id="@+id/container">
<RadioGroup
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radioGroup1"
    android:orientation="horizontal"
    android:gravity="center">
    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/man"
    android:button="@drawable/woman_selector"
    android:radius="50dp"
    android:layout_marginRight="40dp" />
    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/woman"
    android:button="@drawable/man_selector"
    android:radius="50dp"
    android:layout_marginLeft="40dp" />
</RadioGroup>
</LinearLayout>

woman_selextor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selwoman" android:state_checked="true"/>
<item android:drawable="@drawable/defwoman" android:state_checked="false"/>
</selector>

defwoman.xml (selwoman.xml只有不同的颜色)

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" 
                android:id="@+id/shapeBg"> 
            <solid android:color="#B733C6"/>    
            <size android:width="70dp" android:height="70dp"/>
        </shape>
    </item>
    <item>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="70dp"
    android:height="70dp"
    android:viewportWidth="70"
    android:viewportHeight="70">
    <path
        android:fillColor="#ffffff"
        android:pathData="M43 45l-6 0 0 -5c6,-1 11,-6 11,-13 0,-7 -6,-13 -13,-13 -7,0 -13,6 -13,13 0,7
5,12 11,13l0 5 -6 0c-2,0 -2,3 0,3l6 0 0 6c0,2 4,2 4,0l0 -6 6 0c2,0 2,-3
0,-3zm-18 -18c0,-5 5,-10 10,-10 5,0 10,5 10,10 0,5 -5,10 -10,10 -5,0 -10,-5
-10,-10z" />
</vector>
    </item>
</layer-list>

和支持库:

Xamarin.Android.Support.Animated.Vector.Drawable.23.4.0.1Xamarin.Android.Support.v4.23.4.0.1Xamarin.Android.Support.v7.AppCompat.23.4.0.1Xamarin.Android.Support.Vector.Drawable.23.4.0.1

我创建新的应用程序,并重现此错误,链接到App1。sln文件

这完美的工作在Android 5.1,但崩溃的4.4。

AppCompat Vector drawable在RadioButton选择器上崩溃

让Activity派生自AppCompatActivity并添加静态构造函数

    [Activity (Label = "App1.Droid", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : AppCompatActivity
        {
            static MainActivity()  
            {
                AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
            }
            protected override void OnCreate (Bundle bundle)
            {

你将不得不改变manifest从AppCompat派生的主题。你可以选择不同的主题(我喜欢黑色)

    <application android:label="App1.Droid" android:theme="@style/Theme.AppCompat"></application>

在选择器中,用可绘制的逐项向量替换项

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"
            android:id="@+id/shapeBg">
      <solid android:color="#fff"/>
      <size android:width="70dp" android:height="70dp"/>
    </shape>
  </item>
  <item android:drawable="@drawable/woman_path"/>
</layer-list>

woman_path.xml看起来像

之前的向量
<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="70dp"
    android:height="70dp"
    android:viewportWidth="70"
    android:viewportHeight="70">
  <path
      android:fillColor="#ffffff"
      android:pathData="M43 45l-6 0 0 -5c6,-1 11,-6 11,-13 0,-7 -6,-13 -13,-13 -7,0 -13,6 -13,13 0,7
5,12 11,13l0 5 -6 0c-2,0 -2,3 0,3l6 0 0 6c0,2 4,2 4,0l0 -6 6 0c2,0 2,-3
0,-3zm-18 -18c0,-5 5,-10 10,-10 5,0 10,5 10,10 0,5 -5,10 -10,10 -5,0 -10,-5
-10,-10z" />
</vector>