Joey.Z
Joey.Z

Reputation: 4770

setGravity not working for RelativeLayout

This question is similar to mine, but no constructive answer there.

I am trying to do a simple toggle of a header view in a list. I want the head of first section be in the left, and that of the second be in the right. The layout of this head view is a relativelayout as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/location_list_pinned_header"
    android:layout_width="fill_parent"
    android:layout_height="32dip"
    android:background="#505050"
    android:clickable="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/zlocation_list_header_text"
        android:layout_width="40dip"
        android:layout_height="32dip"
        android:layout_gravity="top"
        android:gravity="center"
        android:paddingTop="2dip"
        android:textColor="#000000"
        android:textSize="16sp" />

</RelativeLayout>

And what I do in the java code is:

    RelativeLayout header = (RelativeLayout) headerView.findViewById(R.id.location_list_pinned_header);
    if ( section%2 == 0 ) {
        ((TextView) headerView.findViewById(id))
                .setText(title);//set the section title, may be like "Feb" "Jan"
        header.setGravity(Gravity.LEFT);//I am sure it is been set, I see the mGravity of RelativeLayout changed in debug mode
    }
    else {
        ((TextView) headerView.findViewById(id))
                .setText(title);
        header.setGravity(Gravity.RIGHT);
    }

As I am saying in the comments, I am sure the setGravity has set the value. But it's not changed. Should I perform some other action after I have set the layout param? Because if I have set the gravity to RIGHT in if clause, it is not shown in my screen. But if I scroll down and up again, it changed to right side, but still not as I expected, different position for each section. All section in this case went to right...

headerView is a View that holds the RelativeLayout:

mDisPlay.setPinnedHeaderView(LayoutInflater.from(mContext).inflate(
                    R.layout.zlocation_list_section_header, mDisPlay, false));

will set header view,mDisPlay is the list, like I said before. the layout used in this code is shown in the first excerpt of code. The following is the method setPinnedHeaderView

public void setPinnedHeaderView(View view) {
    mHeaderView = view;
    if (mHeaderView != null) {
        setFadingEdgeLength(0);
    }
    System.out.println("setPinnedHeaderView");

    requestLayout();
}

Upvotes: 2

Views: 1666

Answers (1)

Ken
Ken

Reputation: 31161

Note that RelativeLayout does not have an orientation property; remove this from your XML.

However, I don't see why you need a RelativeLayout parent for the header here; why not use LinearLayout? Give this vertical orientation, say, and set its gravity as you are.

If you can do away with the parent layout altogether so much the better. This one only has a single child.

Upvotes: 2

Related Questions