2布局android系统中的活动之间的切换布局、系统、android

2023-09-05 08:20:08 作者:明天依然很美

我有,我想用修改同一个数据2个不同的布局文件,我切换到我的布局,为编辑视图,允许用户修改图形数据,然后让他们切换回详细视图显示的详细图(使用androidplot库)。

我的问题是,在切换回我的编辑视图时,我的绘制线都不见了,只有轴系画(这样的布局开关和的OnDraw()被调用我的图形视图)。一切都存储在同一个活动中,所以我不明白这是为什么不工作?

的线被存储在图形视图对象本身应该是持续的,因为它是在我的活性的存储变量内

我用这两种方法的切换按钮上点击布局文件。

 公共类GraphLibActivity延伸活动{

    民营图图;

    私人布尔EditView中;

    私有静态的TextView coordTextView;


    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        EditView中= TRUE;

        的setContentView(R.layout.graphlib);



        图=(图)findViewById(R.id.graph);

        coordTextView =(TextView中)findViewById(R.id.currentCoords);


        (大量计算)
        graph.addLine(gelHistogramPoints,linePaint);



        graph.invalidate();
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        MenuInflater充气= getMenuInflater();
        如果(EditView中==真){
            inflater.inflate(R.menu.activity_menu,菜单);
        }其他{
            inflater.inflate(R.menu.detailed_view_menu,菜单);
        }
        返回true;
    }

    公共布尔prepareOptionsMenu(功能菜单)在{
        menu.clear();
        MenuInflater充气= getMenuInflater();
        如果(EditView中==真){
            inflater.inflate(R.menu.activity_menu,菜单);
        }其他{
            inflater.inflate(R.menu.detailed_view_menu,菜单);
        }
        graph.invalidate();
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){

        //处理项目选择
        开关(item.getItemId()){


        案例R.id.detailed_view:
            EditView中= FALSE;
            的setContentView(R.layout.imagegraph);
            返回true;


        案例R.id.edit_view:
            EditView中= TRUE;
            的setContentView(R.layout.editgraph);
            返回true;

        默认:
            返回super.onOptionsItemSelected(项目);
        }

    }




}
 

解决方案

每次调用的setContentView你正在膨胀的布局,数据必须重新设置。你在做什么?

无论如何,我会建议两个布局合并成一个文件。然后使用 ViewFlipper 改变从一种布局到另一个。这看起来类似于:

graph.xml:

 < ViewFlipper机器人:ID =@ + ID / viewFlipper>
    <的LinearLayout>
        //这里会去R.layout.imagegraph的内容
    < / LinearLayout中>
    <的LinearLayout>
        //这里会去R.layout.editgraph的内容
    < / LinearLayout中>
< / ViewFlipper>
 

然后你只需要调用 showNext(),以便在活动切换布局:

  ViewFlipper VF =(ViewFlipper)findViewById(R.id.viewFlipper);
vf.showNext();
 
作者说这是初级Android工程师的面经 吓到我了

希望它帮助。

I have 2 different layout files that I want to use for modifying the same data, I was to switch to my layout for the "edit view" that allows the user to modify graph data and then allow them to switch back to a "detailed view" that displays a detailed graph(using androidplot library).

My issue is, when switching back to my "edit view" my graphed lines are gone and only the axises draw(so the layout switches and the onDraw() is being called for my graph View). Everything is stored within the same Activity so I don't understand why this isn't working?

The lines are stored within the Graph View object itself which should be persistent since it is a stored variable in my activity.

I use these two methods for switching layout files on a button click.

public class GraphLibActivity extends Activity {

    private Graph graph;

    private boolean editView;

    private static TextView coordTextView;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        editView = true;

        setContentView(R.layout.graphlib);



        graph = (Graph) findViewById(R.id.graph);

        coordTextView = (TextView)findViewById(R.id.currentCoords);


        (lots of calculations)
        graph.addLine(gelHistogramPoints, linePaint);



        graph.invalidate();      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu){
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        graph.invalidate();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {


        case R.id.detailed_view:
            editView = false;
            setContentView(R.layout.imagegraph);
            return true;


        case R.id.edit_view:
            editView = true;
            setContentView(R.layout.editgraph);             
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }

    }




}

解决方案

Each time you call setContentView you are inflating the layout so data must be set again. Are you doing that?

Anyway, I would recommend merging the two layouts into one file. Then use ViewFlipper to change from one layout to another. That would look something similar to:

graph.xml:

<ViewFlipper android:id="@+id/viewFlipper">
    <LinearLayout>
        // Here would go the content of R.layout.imagegraph
    </LinearLayout>
    <LinearLayout>
        // Here would go the content of R.layout.editgraph
    </LinearLayout>
</ViewFlipper>

Then you just have to call showNext() to switch layouts in your activity:

ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
vf.showNext();

Hope it helps.