2011-09-08 41 views
0

这是一个重大的编辑对我刚才的问题方向变化不重绘布局完全

我创建一个自定义(复合)组件名为OneLineSeeker,它由一个TextView和搜索栏的。 当我在一个布局中声明多个OneLineSeekers时,它们在配置更改时不保留其状态。所有寻找者的拇指在配置更改时都采用相同的位置(最后一个OneLineSeeker的位置)。 如果我不使用自定义组件,并且单独使用SeekBars(在主要活动中取消注释//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 /布局文件

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 /值文件

attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="OneLineSeeker"> 
     <attr name="name" format="string"/> 
    </declare-styleable> 
</resources> 
+0

OnCreate会重置所有内容,而不需要重置进度。还有其他的错误。 –

+0

如果这仍然隐藏你,尝试绕过整个娱乐过程。这是通过清单中的android:configChanges属性完成的。对于任何类型的配置更改,如果您在那里处理,则会接到对当前活动的onConfigurationChanged(配置)方法的调用,而不是重新启动。所以你可以在onConfigurationChanged中添加登录来重置进度。 –

回答

0

在这u必须确定方向变化的方法.....

public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    onCreate(null); 
} 

试试这个代码可以u得到任何帮助.....

1

它不应该重置其位置归零。这实际上是一个功能。当视图与id相关联时,它通常会保存并在配置更改时恢复其状态。

如果您不希望特定视图在配置更改时保存状态,则可以从布局中的视图条目中删除android:id(但您将无法控制该视图)。另一种选择是扩展ProgressBar并禁用其恢复状态的能力。

最后一个选项是最简单的一个:覆盖onRestoreInstanceState()和复位状态有:

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{ 
    super.onRestoreInstanceState(savedInstanceState); 
    bar.setProgress(0); 
} 

但我会强烈建议不进行重置视图状态。在配置改变之后恢复状态的所有想法都是让用户认为没有什么变化。你需要有非常有力的理由来禁用这种行为。