Android无法使用attrs值为布局文件充气

时间:2019-04-19 19:15:01

标签: java android xml android-layout android-night-mode

我正在开发一个可以在夜间模式和白天模式之间切换的应用程序 我用值制作了一个attrs文件

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

    <declare-styleable name="ds" >

        <attr name="backgroundcolor" format="color"/>
        <attr name="cardbackground" format="color"/>
        <attr name="textcolor" format="color"/>
        <attr name="tintcolor" format="color"/>
        <attr name="buttoncolor" format="color"/>

    </declare-styleable>

</resources>

,我分别设置了两种样式,分别是白天模式和夜间模式

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

    <!-- Base application theme. -->
    <!-- Light Mode -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#1976D2</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundcolor">#fcfcfc</item>
        <item name="cardbackground">#ffffff</item>
        <item name="textcolor">#808080</item>
        <item name="tintcolor">#000000</item>
        <item name="buttoncolor">#2196F3</item>
    </style>

    <!-- Dark Mode -->
    <style name="darktheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#212121</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundcolor">#303030</item>
        <item name="cardbackground">#424242</item>
        <item name="textcolor">#FFFFFF</item>
        <item name="tintcolor">#FFFFFF</item>
        <item name="buttoncolor">#2196F3</item>
    </style>

</resources>

并且我在布局中添加了 attrs 值来代替 background textColor 属性,以便我可以在它们之间进行切换

这是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:minHeight="500dp"
    android:layout_margin="5dp"
    android:layout_height="match_parent"
    android:background="?attr/backgroundcolor">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="7dp"
        card_view:contentPadding="10dp"
        android:background="?attr/cardbackground">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="?attr/backgroundcolor">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/images"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/header"
                android:layout_width="wrap_content"
                android:padding="2dp"
                android:text="Hello"
                android:textColor="?attr/textcolor"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/mNews"
                android:layout_width="wrap_content"
                android:padding="2dp"
                android:layout_height="wrap_content"
                android:text="Asam"
                android:layout_marginTop="10dp"
                android:textSize="16sp"
                android:textStyle="italic"
                android:textColor="?attr/textcolor"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

我正在实现一个ViewPager,在其中我要对该布局文件进行充气,以前却没有实现 attrs ,但效果很好,但是当我现在在内部使用 attrs值时,布局xml文件,它崩溃了应用程序

这是 Viewpager适配器,我在其中放大布局文件

    public class HorizontalPagerAdapter extends PagerAdapter {
    String mResources[] = {"spider man description", "Iron man description", "batman description", "superman description"};
    String mHeader[] = {"Spider-Man", "Iron Man", "BatMan", "Super Man"};
    int mImages[] = {R.drawable.images, R.drawable.ironman, R.drawable.batman, R.drawable.super_man};

    Context mContext;
    LayoutInflater mLayoutInflater;

    public HorizontalPagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mResources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.content_main, container, false);

        TextView newsHeader = (TextView) itemView.findViewById(R.id.header);
        TextView newsContent = (TextView) itemView.findViewById(R.id.mNews) ;
        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);

        newsHeader.setText(mHeader[position]);
        newsContent.setText(mResources[position]);
        imageView.setImageResource(mImages[position]);

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}

因此,在运行应用程序时,它会向我显示此错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.slimshady.newsreaderfinal, PID: 5440
    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.slimshady.newsreaderfinal.HorizontalPagerAdapter.instantiateItem(HorizontalPagerAdapter.java:40)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1010)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1158)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:22071)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:22071)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f030036 a=-1}
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:944)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
        at android.view.View.<init>(View.java:4768)
        at android.view.ViewGroup.<init>(ViewGroup.java:597)
        at android.widget.LinearLayout.<init>(LinearLayout.java:234)
        at android.widget.LinearLayout.<init>(LinearLayout.java:230)
        at android.widget.LinearLayout.<init>(LinearLayout.java:226)
            ... 55 more
Application terminated.

我在这里做错了什么?无法在线找到合适的解决方案,所以我正在创建此线程。

2 个答案:

答案 0 :(得分:1)

膨胀View时,相关属性和值将从Theme使用的Context上的LayoutInflater中提取。在这种情况下,很明显Context消息表明传递给HorizontalPagerAdapter的{​​{1}}上没有正确的Theme

当应用程序Failed to resolve attribute用于创建UI组件时,通常会发生这种情况。默认情况下,即使在清单的Context元素上将应用程序Context指定为Theme,默认情况下,应用程序android:theme并没有附加任何<application>。这只是用来为Theme类设置默认的Activity

通常,您只想将当前Activity用作Context相关的任何事物,实际上,这就是这里的解决方案。 View将附加适当的Activity,然后在Theme中填充这些页面时,您的自定义属性将正确解析。

答案 1 :(得分:0)

不要使用样式元素,请在新的values-night目录中创建另一个styles.xml,并使用更改后的值制作AppTheme的另一个副本。

Full guide