Android 更改方向时保存textview属性

Android 更改方向时保存textview属性,android,landscape,Android,Landscape,我需要将文本视图属性(如文本、颜色、背景色、填充值)保存在onSaveInstanceState()- textviewtime位于适配器类的bindView()中- viewHolder.time.setText(strText); viewHolder.time.setTextColor(0xff000000); viewHolder.time.setTextSize(17); viewHolder.time.setVisibility(View.VISIBLE); viewHolder.t

我需要将文本视图属性(如文本、颜色、背景色、填充值)保存在
onSaveInstanceState()
-

textview
time
位于适配器类的
bindView()
中-

viewHolder.time.setText(strText);
viewHolder.time.setTextColor(0xff000000);
viewHolder.time.setTextSize(17);
viewHolder.time.setVisibility(View.VISIBLE);
viewHolder.time.setBackgroundColor(nColor);
viewHolder.time.setPadding(25,25,25,25);
当方向更改为横向时,如何将它们保存在
onSaveInstanceState()
中并在
onCreate()
中使用


我不想使用android:configChanges,因为我的横向布局不同

编辑

在MyAdapter.java中

@Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        Child child = children.createStopFromCursor(cursor);
        MyFragment ndf = new MyFragment();   
                    viewHolder.time.setText(strText);
                    viewHolder.time.setTextColor(0xff000000);
                    viewHolder.time.setTextSize(17);
                    viewHolder.time.setVisibility(View.VISIBLE);
                    viewHolder.time.setBackgroundColor(nColor);
                    viewHolder.time.setPadding(25,25,25,25);
                    ndf.setLandScape(strText,0xff000000,17,nColor);
                    view.invalidate();
        }
    }
在MyFragment.java中

public void setLandScape(String time,int time_color,int time_size,int time_Bcolor){
        this.delay_time = time;
        this.delay_time_color = time_color;
        this.delay_time_size = time_size;
        this.delay_time_BColor = time_Bcolor;
      }

@Override
    public void onSaveInstanceState(Bundle outState) {
        int[] padding = {25,25,25,25};
        TextViewLandscape attributes = new TextViewLandscape(delay_time, delay_time_color, delay_time_size, delay_time_BColor, padding);
        ArrayList<TextViewLandscape> list = new ArrayList<TextViewLandscape>();
        list.add(attributes);
        outState.putParcelableArrayList("keyTextViewAttributes", list);
        super.onSaveInstanceState(outState);
    }

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mFromSavedInstanceState = true;
            TextViewLandscape textViewAttributes = savedInstanceState.getParcelable("keyTextViewAttributes");
        }}
public void setscape(字符串时间、整数时间颜色、整数时间大小、整数时间颜色){
这个。延迟时间=时间;
这个。延迟时间颜色=时间颜色;
此参数。延迟时间大小=时间大小;
this.delay\u time\u BColor=time\u BColor;
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
int[]padding={25,25,25};
textView横向属性=新的textView横向(延迟时间、延迟时间颜色、延迟时间大小、延迟时间颜色、填充);
ArrayList=新建ArrayList();
列表。添加(属性);
putParcelableArrayList(“keyTextViewAttributes”,列表);
super.onSaveInstanceState(超出状态);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(savedInstanceState!=null){
mFromSavedInstanceState=true;
textView横向textViewAttributes=savedInstanceState.getParcelable(“keyTextViewAttributes”);
}}
在TextViewLandscape.java中

public class TextViewLandscape implements Parcelable {
    private String text;
    private int textColor;
    private int textSize;
    private int backgroundColor;
    private int[] paddingAttrs = {4};

    public TextViewLandscape(String text, int textColor, int textSize, int backgroundColor, int[] paddingAttrs) {
        this.text = text;
        this.textColor = textColor;
        this.textSize = textSize;
        this.backgroundColor = backgroundColor;
        this.paddingAttrs = paddingAttrs;
    }

    public TextViewLandscape(Parcel in) {
        text = in.readString();
        textColor = in.readInt();
        textSize = in.readInt();
        backgroundColor = in.readInt();
        paddingAttrs = in.createIntArray();
    }

    public static final Creator<TextViewLandscape> CREATOR = new Creator<TextViewLandscape>() {
        @Override
        public TextViewLandscape createFromParcel(Parcel in) {
            return new TextViewLandscape(in);
        }

        @Override
        public TextViewLandscape[] newArray(int size) {
            return new TextViewLandscape[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(text);
        dest.writeInt(textColor);
        dest.writeInt(textSize);
        dest.writeInt(backgroundColor);
        dest.writeIntArray(paddingAttrs);
    }
}
public类textView实现可包裹{
私有字符串文本;
私有int文本颜色;
私有int文本大小;
私人背景色;
私有int[]paddingAttrs={4};
公共文本视图景观(字符串文本、int-textColor、int-textSize、int-backgroundColor、int[]填充字符串){
this.text=文本;
this.textColor=textColor;
this.textSize=textSize;
this.backgroundColor=backgroundColor;
this.paddingAttrs=paddingAttrs;
}
公共文本视图景观(地块中){
text=in.readString();
textColor=in.readInt();
textSize=in.readInt();
backgroundColor=in.readInt();
paddingAttrs=in.createIntArray();
}
公共静态最终创建者=新创建者(){
@凌驾
公共文本视图横向创建自地块(地块位于){
返回新的文本视图景观(在中);
}
@凌驾
公共文本视图[]新数组(整数大小){
返回新的文本视图横向[大小];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的书面限制(文本);
目标写入(文本颜色);
目的写入(textSize);
目的写入(背景色);
目的写入阵列(填充阵列);
}
}

仍然无法在横向模式下工作….

只需在
activity
中添加
configChanges
,在
AndroidManifest.xml
中添加标记,如下所示:

<activity
    android:name=".TextureViewActivity"
    android:configChanges="orientation|screenSize"/>

OnSaveInstanceState()。缺点是您必须使用大量的键来表示值,并且在某些情况下可能会变得混乱

通过检查savedInstanceState是否为NULL并使用之前使用的相同键,在onCreateView()中还原它们

编辑:
事实上,您实际上不需要保存这些值。当屏幕方向更改时,将重新创建包含适配器的片段以及适配器。但是您的值在适配器中是硬编码的。因此,您不需要保存它们。

在这种情况下,您将必须定义一些保存所有
TextView
属性的可包裹类,例如:

 public class TextViewAttributes implements Parcelable {
    private String text;
    private int textColor;
    private int textSize;
    private int visibility;
    private int backgroundColor;
    private int[] paddingAttrs = {4};

    public TextViewAttributes(String text, int textColor, int textSize, int visibility, int backgroundColor, int[] paddingAttrs) {
        this.text = text;
        this.textColor = textColor;
        this.textSize = textSize;
        this.visibility = visibility;
        this.backgroundColor = backgroundColor;
        this.paddingAttrs = paddingAttrs;
    }

    public TextViewAttributes(Parcel in) {
        text = in.readString();
        textColor = in.readInt();
        textSize = in.readInt();
        visibility = in.readInt();
        backgroundColor = in.readInt();
        paddingAttrs = in.createIntArray();
    }

    public static final Creator<TextViewAttributes> CREATOR = new Creator<TextViewAttributes>() {
        @Override
        public TextViewAttributes createFromParcel(Parcel in) {
            return new TextViewAttributes(in);
        }

        @Override
        public TextViewAttributes[] newArray(int size) {
            return new TextViewAttributes[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(text);
        dest.writeInt(textColor);
        dest.writeInt(textSize);
        dest.writeInt(visibility);
        dest.writeInt(backgroundColor);
        dest.writeIntArray(paddingAttrs);
    }
}

为什么不将它们保存在绑定到的对象中?它们是否依赖于某些属性,例如boolean isFavorited?适配器将在onCreate()上重新加载,因此列表将再次填充,您如何维护它?使用相同选项保存高亮显示(选定)的属性textView@F.sh如何做到这一点?请参阅我的更新答案,在
onCreate
中,只需更改
textview横向textviewtattributes=savedInstanceState.getParcelable(“keytextviewtributes”)
to
ArrayList textViewAttributes=savedInstanceState.getParcelableArrayListExtra(“keyTextViewAttributes”)
我无法使用getParcelableArrayListExtra。上面说无法解决方法我有不同的景观布局OK。。。我可以使用哪些键来进行可见性和填充,这些键位于适配器类中。我怎么把它弄成碎片class@Mark023检查新的编辑器如果我不需要保存它们,那么为什么在屏幕定向之后我得不到这些值。当你再次通过适配器时,你的textColor、textSize、visibility和padding值已经硬编码了。我可以假设您从字符串或类似的数据集获取strText。您需要从片段中保存该数据集,而不是尝试保存TextView的属性,因为您已经有了这些属性。您将必须创建从片段到活动的回调,请检查这一点,为什么在onSaveInstanceState()中需要包变量“dest”我没有使用itI,我实现了您上面给出的所有3个部分…但它仍然不起作用抱歉,我犯了一个小错误,在
onCreate
中,而不是
savedInstanceState.getParcelable(“keyTextViewAttributes”)
调用
savedInstanceState.getParcelableArrayListExtra(“keyTextViewAttributes”)
。现在行了吗?还是没有结果。。。我要发布我正在做的事情吗?
@Override
    protected void onSaveInstanceState(Bundle outState) {
        int[] padding = {0,5,1,3};
        TextViewAttributes attributes = new TextViewAttributes("message", 4, 5, 7, 1, padding);
        ArrayList<TextViewAttributes> list = new ArrayList<TextViewAttributes>();
        list.add(attributes);
        outState.putParcelableArrayList("keyTextViewAttributes", list);
        super.onSaveInstanceState(outState);
    }
 if (savedInstanceState != null) {
            ArrayList<TextViewAttributes> textViewAttributes = savedInstanceState.getParcelableArrayListExtra("keyTextViewAttributes");
        }