方向更改不会完全重绘布局
这是对我上一个问题的主要编辑
我正在创建一个名为 OneLineSeeker 的自定义(复合)组件,它由 TextView 和 SeekBar 组成。 当我在布局中声明多个 OneLineSeeker 时,它们不会在配置更改时保留其状态。在配置更改时,所有导引头拇指均采用相同的位置(最后一个 OneLineSeeker 的位置)。 如果我不使用自定义组件而仅单独使用 SeekBar(在主活动中取消注释 //a()
),它们将在配置更改时保留其各自的位置。 有人可以帮忙吗?
src 文件
A_seekbarsActivity.java
package com.an;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class A_seekbarsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//a();
}
void a() {
setContentView(R.layout.main_1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SeekBar sb = (SeekBar) findViewById(R.id.seekBar1);
if(sb.getProgress() > 50) sb.setProgress(0);
else sb.incrementProgressBy(5);
}
});
}
}
OneLineSeeker.java
package com.an;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class OneLineSeeker extends LinearLayout {
public OneLineSeeker(Context context, AttributeSet attrs) {
super(context, attrs);
View ols = LayoutInflater.from(context).inflate(R.layout.one_line_seeker, this, true);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OneLineSeeker, 0, 0);
String text = array.getString(R.styleable.OneLineSeeker_name);
if (text != null)
((TextView) ols.findViewById(R.id.name)).setText(text);
array.recycle();
}
}
res/layout 文件
main_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SeekBar android:id="@+id/seekBar1" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="@+id/seekBar2" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="@+id/seekBar3" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:oneLineSeeker="http://schemas.android.com/apk/res/com.an"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.an.OneLineSeeker
android:id="@+id/s1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="1"
/>
<com.an.OneLineSeeker
android:id="@+id/s2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="2"
/>
<com.an.OneLineSeeker
android:id="@+id/s3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name='3'
/>
</LinearLayout>
one_line_seeker.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id='@+id/name'
android:text='name'
/>
<SeekBar
android:id='@+id/seekbar'
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</merge>
res/values 文件
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OneLineSeeker">
<attr name="name" format="string"/>
</declare-styleable>
</resources>
This is a Major Edit on my previous question
I'm creating a custom (composite) component named OneLineSeeker that consists of a TextView and a SeekBar.
When I have multiple OneLineSeekers declared in a layout, they don't retain their state on configuration change. All seeker thumbs assume the same position (the position of the last OneLineSeeker) on configuration change.
If I don't use the custom component and use only SeekBars individually (uncomment //a()
in main activity), they will retain their individual positions on configuration change.
Can anyone help?
src files
A_seekbarsActivity.java
package com.an;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class A_seekbarsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//a();
}
void a() {
setContentView(R.layout.main_1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SeekBar sb = (SeekBar) findViewById(R.id.seekBar1);
if(sb.getProgress() > 50) sb.setProgress(0);
else sb.incrementProgressBy(5);
}
});
}
}
OneLineSeeker.java
package com.an;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class OneLineSeeker extends LinearLayout {
public OneLineSeeker(Context context, AttributeSet attrs) {
super(context, attrs);
View ols = LayoutInflater.from(context).inflate(R.layout.one_line_seeker, this, true);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OneLineSeeker, 0, 0);
String text = array.getString(R.styleable.OneLineSeeker_name);
if (text != null)
((TextView) ols.findViewById(R.id.name)).setText(text);
array.recycle();
}
}
res/layout files
main_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SeekBar android:id="@+id/seekBar1" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="@+id/seekBar2" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="@+id/seekBar3" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:oneLineSeeker="http://schemas.android.com/apk/res/com.an"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.an.OneLineSeeker
android:id="@+id/s1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="1"
/>
<com.an.OneLineSeeker
android:id="@+id/s2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="2"
/>
<com.an.OneLineSeeker
android:id="@+id/s3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name='3'
/>
</LinearLayout>
one_line_seeker.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id='@+id/name'
android:text='name'
/>
<SeekBar
android:id='@+id/seekbar'
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</merge>
res/values files
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OneLineSeeker">
<attr name="name" format="string"/>
</declare-styleable>
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不应该将其位置重置为零。这实际上是一个功能。当视图与
id
关联时,它通常会在配置更改时保存并恢复其状态。如果您不希望特定视图在配置更改时保存状态,您可以从布局中视图的条目中删除
android:id
(但此时您将无法控制该视图)。另一种选择是扩展ProgressBar
并禁用其恢复状态的能力。最后一个选项是最简单的一个:覆盖
onRestoreInstanceState()
并在那里重置状态:但我强烈建议不要重置视图状态。毕竟,在配置更改时恢复状态的整个想法是让用户认为好像没有真正发生任何变化。并且您需要有非常充分的理由来禁用此行为。
Its not supposed to reset its position to zero. This is actually a feature. When view has
id
associated with, it usually saves and the restores its state on configuration changes.If you don't want specific view to save state on configuration change, you can remove
android:id
from view's entry in layout (but you won't be able to control that view then). Another alternative is to extendProgressBar
and disable its ability to restore state.The last one option is the easiest one: override
onRestoreInstanceState()
and reset state there:But I would strongly recommend not resetting view state. After all the whole idea of restoring state on configuration change is to make user think as if nothing really changed. And you need to have very strong reasons to disable this behavior.
因为你必须定义方向改变方法......
尝试这个代码你可能会得到任何帮助......
In that u have to define Orientation Change Methods.....
Try this Code may u get any Help.....