简体   繁体   中英

How to set the tablelayout android:layout_below programmatically

I have 2 table layout . I want to hide the second layout and move the third layout to second position.

I'm hiding the the second layout and but don't know how to move the third layout to second position

            TableLayout i=(TableLayout)findViewById(R.id.table2);
    TableLayout i2=(TableLayout)findViewById(R.id.table3);
    TableLayout i3=(TableLayout)findViewById(R.id.table4);
    i.setVisibility(View.GONE);
    i2.setVisibility(View.GONE);
    i3.setVisibility(View.GONE);
    TableLayout i5=(TableLayout)findViewById(R.id.table5);
             i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)                 

使用表布局的view.removeView(id)然后当你想使用view.addView(id)

尝试设置

i2.setVisibility(View.INVISIBLE);

You can do it like, as you said you are using RelativeLayout, so make a child LinearLayout with vertical orientation, then add all the table layouts you want to add.

Bow if you want to add a tableLayout at runtime at the first position, then do:

linearLayout.removeAllViews();

Then add the required TableLayout(here, i5) and after that add the remaining Layouts.

Edit: Your code:

TableLayout i=(TableLayout)findViewById(R.id.table2);
TableLayout i2=(TableLayout)findViewById(R.id.table3);
TableLayout i3=(TableLayout)findViewById(R.id.table4);
i.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
TableLayout i5=(TableLayout)findViewById(R.id.table5);
         i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)  

Are you going right, as i think your code should look like:

TableLayout i=(TableLayout)findViewById(R.id.table1);
TableLayout i2=(TableLayout)findViewById(R.id.table2);
TableLayout i3=(TableLayout)findViewById(R.id.table3);
i.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
TableLayout i5=(TableLayout)findViewById(R.id.table5);
         i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)  

I suggested this because of your comment section after i5 in your above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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