Eugene Chumak
Eugene Chumak

Reputation: 3192

RelativeLayout -> match_parent is not the same size with parent view

I've made a simple application to illustrate my problem. Brief explanation: the app contains only one activity. It's root view is TableLayout. The latter is filled with TableRow elements which are, in turn, inflated from "row.xml". Here is code:

TestActivity.java

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater=getLayoutInflater();
    TableLayout table=new TableLayout(this);
    setContentView(table);
    TableRow row; 
    for(int i=0;i<3;i++){
        row=(TableRow)inflater.inflate(R.layout.row, null, true);
        table.addView(row);
    }

  }
}

In row.xml TableRow is root element, it has a child - RelativeLayout. Pay attention to RelativeLayout's property android:layout_width="match_parent". row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
>

<RelativeLayout
    android:id="@+id/announcement_row_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
</RelativeLayout>

Then I run the project, open hierarchyViewer tool and inspect width of table rows and corresponding relative layouts - I see 320 and 0 respectively. Why aren't they the same (320 and 320) if I declared "match_parent" in relative layout? I attach 2 images where you can observe width of the elements by your own.

TableRow width RelativeLayout width

Upvotes: 2

Views: 2227

Answers (2)

Cata
Cata

Reputation: 11211

Your problem is here : row=(TableRow)inflater.inflate(R.layout.row, null, true);

You have to inflate your row like this:

row=(TableRow)inflater.inflate(R.layout.row, table, false);

This way the LayoutParams will get loaded from your XML file.

Upvotes: 1

a.ch.
a.ch.

Reputation: 8390

This happens because your RelativeLayout contains no children and its onMeasure(..) method hasn't provided any desired width.

Upvotes: 1

Related Questions