简体   繁体   中英

TableLayout - TextView and ImageView inline

I have built an app that dynamically adds TextViews and ImageViews into the TableLayout. I am facing one small issue, I can't get ImageView to be displayed inline with TextView fields. In my fragment I get the results back from a server and display it in the layout.

I have tried several ways such as setting the gravity of TextView to Center vertical like this
I tried limiting the width of the text fields but it won't work.

    firstRow.setGravity(Gravity.CENTER_VERTICAL);

This is how my rows are populated:

    businessNameRow.addView(businessNameField);

I am adding sets dynamically, each set consists of 7 TextView rows and 1 ImageView in each row as follows:

TableRow firstRow = new TableRow(getActivity().getApplicationContext());
TableRow secondRow = new TableRow(getActivity().getApplicationContext());
TableRow thirdRow = new TableRow(getActivity().getApplicationContext());
TableRow fourthRow = new TableRow(getActivity().getApplicationContext());
TableRow fifthRow = new TableRow(getActivity().getApplicationContext());
TableRow sixthRow = new TableRow(getActivity().getApplicationContext());
TableRow imageRow = new TableRow(getActivity().getApplicationContext());
TableRow seventhRow = new TableRow(getActivity().getApplicationContext());

Then I just add the row to the table

final TableLayout tl = (TableLayout)root.findViewById(R.id.tableResultsGPS);

tl.addView(firstRow); //And so on

My TableLayout is placed within ScrollView so I can scroll through list of results.

Attaching image of what I want to achieve and what it looks like now. 在此处输入图片说明

Seems like what you are trying to achieve is more suitable with a ListView implementation.

The general idea:

  • Create a list item with the layout of an individual item (your textview and image)
  • Create a new class that models your each item
  • Using a list view, bind the data to the ListView.

There are many tutorials out there. Try this

Side Note: Good suggestion for layout choice would be relative layout for your ListView item. One of my personal favorite because of its flexiblity.

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