lundi 11 mai 2015

Want to align linearlayout inside another, but setGravity() doesn`t work

I am trying to align a linear layout inside another linear layout programmatically, but setGravity(Gravity.RIGHT) doesn`t work.

I know the differences between gravity and layout_gravity.

Here's the xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:id="@+id/initial_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:id="@+id/conversationwrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bubble_yellow"
        >

        <TextView
            android:id="@+id/messagecontent_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:paddingLeft="10dip"
            android:text="Message_content"
            android:textSize="15dp" />
        <TextView
            android:id="@+id/timestamp_tv"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="HH:MM"
            android:ems="2"
            android:textSize="12dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

And the code of getView() in my Adapter:

 public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listitem_conversation, parent, false);
    }
    wrapper = (LinearLayout) row.findViewById(R.id.conversationwrapper);
    initial_wrapper = (LinearLayout) row.findViewById(R.id.initial_wrapper);
    time = (TextView) row.findViewById(R.id.timestamp_tv);

    Message coment = getItem(position);

    content = (TextView)row.findViewById(R.id.messagecontent_tv);


    content.setText(coment.content);
    time.setText(getCurrentTimeStamp());

    wrapper.setBackgroundResource(!coment.mine  ? R.drawable.bubble_yellow :     R.drawable.bubble_green);
    initial_wrapper.setGravity(!coment.mine  ? Gravity.LEFT :  Gravity.RIGHT);


    return row;
}

But it always happen to be on the left, even when the image is green.

The strange thing is that if I type

android:gravity="right"

inside initial_wrapper, It works in the preview but not in the device.

I'm using a Moto G and a Nexus 5, but neither of them work

Aucun commentaire:

Enregistrer un commentaire