RecyclerView中项目的layout_weight和layout_gravity不起作用

时间:2018-09-18 00:14:09

标签: android xml

我的目的是使RecyclerView的各项按以下方式对齐:

  • 两个TextView都向左对齐,
  • EditText右对齐
  • 第一个TextView占据屏幕的1/5,第二个TextView占据屏幕的3/5,而EditText占据屏幕的最后5th。

但是由于某些原因,它无法正常工作。

这是项目的布局:

<?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>

哪个包含在活动的ViewPager中:

<?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>

所有项目均以编程方式添加。

尽管有我的意图,最终结果还是决定忽略layout_weightlayout_gravity的两个属性,如下所示:

Running the application

我将很乐意提供任何需要的其他信息。

1 个答案:

答案 0 :(得分:1)

确保您的LinearLayout根目录的宽度为match_parent,否则权重将不起作用:

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

    ...