Android无法在适配器布局内膨胀可设置样式的属性

时间:2019-04-20 08:22:00

标签: java android xml android-layout android-adapter

我正在尝试在正在使用的应用中实施日/夜模式 为达到目的,我在 styles.xml 中创建了两个主题,白天和夜晚使用不同的颜色

for(i = 0; i < linecount; i++){

for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}
for(i2 = regex iterator;i2 != regex iterator;i2++){
    for(i3 = 0; i3 < termstorage; i3++){

    }
}

这是attrs.xml文件,我在其中实现了可样式化

<?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 ,因此我可以动态更改主题,这是适配器的布局文件

<?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>

但是当我在适配器中填充布局文件时,它给了我 InflationError 并导致应用崩溃

这是适配器文件的Java代码

<?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>

由于充气时 atts 而给出错误,这是堆栈跟踪

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);
    }
}

我该如何解决?

0 个答案:

没有答案