简体   繁体   English

在 android 活动中切换 2 个布局

[英]Switching between 2 layouts in android activity

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).我有 2 个不同的布局文件,我想用它们来修改相同的数据,我要切换到“编辑视图”的布局,允许用户修改图形数据,然后允许他们切换回“详细视图” " 显示详细图表(使用 androidplot 库)。

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).我的问题是,当切换回我的“编辑视图”时,我的图形线消失了,只有轴绘制(因此布局切换和 onDraw() 正在为我的图形视图调用)。 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.这些线存储在图形视图 object 本身中,它应该是持久的,因为它是我活动中的存储变量。

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.每次调用 setContentView 时,您都在膨胀布局,因此必须再次设置数据。 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.然后使用ViewFlipper从一种布局更改为另一种布局。 That would look something similar to:这看起来类似于:

graph.xml:图.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:然后你只需要调用showNext()来切换你的活动中的布局:

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

Hope it helps.希望能帮助到你。

Why are you always inflating the layout?为什么你总是夸大布局? That is really expensive.那真的很贵。 Try to use the merge layout.尝试使用合并布局。 This way, you can change the visibilty to "Gone" or "Visible".这样,您可以将可见性更改为“已消失”或“可见”。 see more: http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html查看更多: http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

I do something similar for an app I'm working on.我为我正在开发的应用程序做了类似的事情。

A simple way to solve it is to set the default visibility of the 2nd view to android:visibility="GONE" in XML, then identify each view in code.解决它的一个简单方法是将第二个视图的默认可见性设置为 XML 中的android:visibility="GONE" ,然后在代码中识别每个视图。 You can then set the visibility with view1.setVisibility(View.GONE) and view2.setVisibility(View.VISIBLE)然后,您可以使用view1.setVisibility(View.GONE)view2.setVisibility(View.VISIBLE)设置可见性

Hope this helps: It worked great for me :)希望这会有所帮助:它对我很有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM