CoordinatorLayout 和权重
CoordinatorLayout and weight
我需要创建一个具有线性布局的 Coordinatorlayout 和一个具有相同 space 的 RecyclerView。
我试过设置 layout_weight 0.5,但 RecyclerView 占用了所有 space。
这是我的代码。
<?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>
我怎样才能做到这一点?
此致,迭戈。
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>
您需要在 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>
你可以这样使用权重
<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" />
我需要创建一个具有线性布局的 Coordinatorlayout 和一个具有相同 space 的 RecyclerView。
我试过设置 layout_weight 0.5,但 RecyclerView 占用了所有 space。
这是我的代码。
<?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>
我怎样才能做到这一点?
此致,迭戈。
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>
您需要在 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>
你可以这样使用权重
<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" />