我需要保存textview属性,例如文本,颜色,背景颜色,填充值onSaveInstanceState()
-
textview time
位于Adapter类的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");
}}
在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);
}
}
仍未在横向模式下工作....
答案 0 :(得分:1)
在这种情况下,您必须定义一些Parcelable类,它保留所有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);
}
}
这是保存值的方法:
@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);
}
这就是你在onCreate
方法中检索它们的方法:
if (savedInstanceState != null) {
ArrayList<TextViewAttributes> textViewAttributes = savedInstanceState.getParcelableArrayListExtra("keyTextViewAttributes");
}
答案 1 :(得分:0)
只需在configChanges
中的activity
标记中添加AndroidManifest.xml
作为gien,如下所示:
<activity
android:name=".TextureViewActivity"
android:configChanges="orientation|screenSize"/>
答案 2 :(得分:0)
onSaveInstaceState()可以毫无问题地保存所有内容。缺点是你必须使用大量的键来表示值,并且在某些时候它会变得混乱。
通过检查 savedInstanceState 是否 NULL 并使用我之前使用的相同密钥,在 onCreateView()中恢复它们。 / p>
修改强> 实际上,您并不需要保存这些值。更改屏幕方向时,将重新创建包含适配器的片段以及适配器。但是您在适配器中对您的值进行了硬编码。因此,您无需保存它们。