How can i change included xml layout to another layout on java code?

前端 未结 3 1078
灰色年华
灰色年华 2020-12-13 01:20

i have a problem. I develop android application.

I included second layout to first layout like this:

         


        
相关标签:
3条回答
  • 2020-12-13 01:44

    I suggest ViewFlipper inside RelativeLayout of your include statements. Try like this:

    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/vf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <include android:id="@+id/include1" layout="@layout/ikinci" />
        <include android:id="@+id/map" layout="@layout/third_layout" />
    
    </ViewFlipper>
    

    Access ViewFlipper as below. Initially first layout is output:

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

    For Button onClickListener:

    button.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    vf.setDisplayedChild(1);
                }
            });
    
    0 讨论(0)
  • 2020-12-13 01:49

    There is two ways to change layouts in code:

    A. Change visibility. Include tow different layouts in your xml:

    <include 
        android:id="@+id/id1"
        android:visibility="visible"/>
    <include 
        android:id="@+id/id2"
        android:visibility="gone"/>
    

    and in code use this:

    findViewById(R.id.id1).setVisibility(View.GONE);
    findViewById(R.id.id2).setVisibility(View.VISIBLE);
    

    B. Manually add children. Use the same xml as you use now and add in code you can add or delete children:

    yourRelativeLayout.removeAllViews();
    yourRelativeLayout.addView(viewToInclude);
    

    Offtopic:

    You don't need to write xmlns:android param in RelativeLayout. Just for the most top tag in layout file.

    0 讨论(0)
  • 2020-12-13 01:56

    Have you tried to get the instance of the current view of the xml you have included the other xml and use findViewById(). View v is the first layout and against this view you use .findViewById()

    0 讨论(0)
提交回复
热议问题