我的目的是使RecyclerView的各项按以下方式对齐:
但是由于某些原因,它无法正常工作。
这是项目的布局:
<?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_weight
和layout_gravity
的两个属性,如下所示:
我将很乐意提供任何需要的其他信息。
答案 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">
...