CoordinatorLayout和重量

问题描述 投票:1回答:3

我需要创建一个带有线性布局的Coordinatorlayout和一个具有相同空间的RecyclerView。

我尝试过设置layout_weight 0.5,但RecyclerView占用了所有空间。

这是我的代码。

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

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@android:color/holo_green_light">

     </LinearLayout>

     <android.support.v7.widget.RecyclerView
        android:id="@+id/lvToDoList"            
        android:layout_weight="0.5"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </android.support.v7.widget.RecyclerView>

     <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@android:drawable/ic_input_add" />

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

我怎样才能做到这一点?

问候,迭戈。

android android-recyclerview android-coordinatorlayout
3个回答
1
投票

layout_weight仅在LinearLayout中工作。因此,将LinearLayout作为中间层:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:background="@android:color/holo_green_light">
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lvToDoList"
            android:background="@android:color/holo_blue_bright"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            >
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@android:drawable/ic_input_add" />

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

0
投票

您需要在CoordinatorLayout中包含LinearLayout。内部LinearLayout提供了钩子,以便Android可以进行加权视图。

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

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@android:color/holo_green_light">

     </LinearLayout>

     <android.support.v7.widget.RecyclerView
        android:id="@+id/lvToDoList"            
        android:layout_weight="0.5"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </android.support.v7.widget.RecyclerView>

     <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@android:drawable/ic_input_add" />

     </LinearLayout>

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

0
投票

你可以使用这样的重量

<android.support.constraint.ConstraintLayout 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_marginTop="@dimen/margin_10_dp"
android:layout_height="wrap_content">

<View
    android:id="@+id/view1"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#212121"
    android:text="Button 1"
    app:layout_constraintEnd_toStartOf="@+id/view2"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent" />

<View
    android:id="@+id/view2"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#3cff42"
    android:text="Button 2"
    app:layout_constraintEnd_toStartOf="@+id/view3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view1" />

<View
    android:id="@+id/view3"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#f93d3d"
    android:text="Button 3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view2" />

© www.soinside.com 2019 - 2024. All rights reserved.