Ren
Ren

Reputation: 4680

layout_weight and layout_gravity of Items within RecyclerView not working

My purpose is to have the items of the RecyclerView aligned in the following manner:

However for some reason it's not working.

Here is the Layout for the item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/item_number"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:layout_margin="@dimen/text_margin"
        android:layout_weight="1"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:layout_margin="@dimen/text_margin"
        android:layout_weight="3"
        android:labelFor="@id/editVote"
        android:textAppearance="?attr/textAppearanceListItem" />

    <EditText
        android:id="@+id/editVote"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|end"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number"
        android:maxLength="7"
        android:layout_margin="@dimen/text_margin"/>

</LinearLayout>

Which is contained within a ViewPager in an activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tabanim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabanim_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/tabanim_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

All items are added programmatically.

Despite my intentions, the end result decides to ignore both the attributes of layout_weight and layout_gravity as shown:

Running the application

Any extra information needed I will be happy to provide.

Upvotes: 1

Views: 523

Answers (1)

TheWanderer
TheWanderer

Reputation: 17854

Make sure your LinearLayout root's width is match_parent, otherwise weights will do nothing:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    ...

Upvotes: 1

Related Questions