【发布时间】:2018-02-14 13:47:35
【问题描述】:
我正在尝试根据平板电脑上的图片构建屏幕。 在片段内部,我有一个带有项目的 RecyclerView。每个项目都有一个 LinearLayout 作为根。并且恰好有 8 个孩子水平分布,权重为 1(如下面的代码所示)。每行的第一个单元格是 TextView,其他单元格是 CustomView(垂直的 LinearLayout 和 2 个 TextViews)。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" />
<TextView
android:id="@+id/item_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/item_border"
android:drawableTop="@drawable/ic_id"
android:fontFamily="@font/cabin_bold"
android:gravity="center"
android:padding="5dp"
android:text="@string/mock"
android:textColor="@android:color/black"
android:textSize="17sp" />
<CustomView X 7 times
android:id="@+id/item_attr1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="1"
app:titleText="Mock" />
</LinearLayout>
鉴于此,如您在图片中看到的那样,该行的子项在宽度上没有正确分布。我已经尝试将weightSum 属性设置为行的根LinearLayout,但没有运气。我注意到(尤其是)当行的第一个子项(TextView)有很多字符时会出现此问题。
这是片段的布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F1F1F1">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:layoutManager="LinearLayoutManager" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="30dp"
android:src="@android:drawable/ic_input_add"
android:tint="@android:color/white"
app:backgroundTint="@android:color/holo_blue_dark"
app:fabSize="normal" />
<TextView
android:id="@+id/loading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/roboto_light"
android:text="@string/loading"
android:textSize="23sp"
android:visibility="gone" />
</android.support.design.widget.CoordinatorLayout>
感谢任何想法
【问题讨论】:
-
尝试在外部布局中添加
android:weightSum="8"。 -
您需要所有 textView 的权重相同,并且所有 custormView 的权重相同。检查所有 textview 的 weight = 3,所有 customView 的 weight = 1
-
@ADM 我已经试过 android:weightSum="8",没区别
-
你能发布所有参数的布局吗?
-
@Kelevandos 我已经更新了代码。 CustomView 一共被乘了 7 次(除了每个的 id,ofc)。
标签: android android-layout android-linearlayout android-layout-weight