Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 4a37cdf

Browse files
committed
[ADDED] [#47] Weighted items for chains.
1 parent 3e16e2e commit 4a37cdf

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

app/src/main/java/com/hossainkhan/android/demo/data/LayoutDataStore.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ class LayoutDataStore @Inject constructor(
8686
" * CHAIN_SPREAD -- the elements will be spread out (default style)\n" +
8787
" * Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space\n" +
8888
" * CHAIN_SPREAD_INSIDE -- similar, but the endpoints of the chain will not be spread out\n" +
89-
" * CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements")
89+
" * CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements"),
90+
LayoutInformation(
91+
layoutResourceId = R.layout.preview_chain_weighted,
92+
thumbnailResourceId = R.drawable.thumb_chain_style,
93+
title = "Chain: Weighted Width or Height",
94+
description = "The default behavior of a chain is to spread the elements equally in the available space. If one or more " +
95+
"elements are using MATCH_CONSTRAINT (0dp), they will use the available empty space (equally divided among " +
96+
"themselves). The attribute `layout_constraintHorizontal_weight` and " +
97+
"`layout_constraintVertical_weight` will control how the space will be distributed among the elements " +
98+
"using MATCH_CONSTRAINT." +
99+
"\n\n" +
100+
"For example, on a chain containing two elements using MATCH_CONSTRAINT, with " +
101+
"the first element using a weight of 2 and the second a weight of 1, the space occupied by the first element" +
102+
"will be twice that of the second element.")
90103
/*
91104
Next item template (easy to copy and paste)
92105
LayoutInformation(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
~ Copyright (c) 2018 Hossain Khan
5+
~
6+
~ Licensed under the Apache License, Version 2.0 (the "License");
7+
~ you may not use this file except in compliance with the License.
8+
~ You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
19+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
xmlns:tools="http://schemas.android.com/tools"
22+
android:id="@+id/constraint_layout_root"
23+
android:layout_width="match_parent"
24+
android:layout_height="match_parent">
25+
26+
<!--
27+
NOTE: All the views width are set to `0dp` to indicate they are
28+
`MATCH_CONSTRAINT` horizontally.
29+
-->
30+
31+
<View
32+
android:id="@+id/view_chain_view_first"
33+
style="@style/MediumBox"
34+
android:layout_width="0dp"
35+
app:layout_constraintEnd_toStartOf="@+id/view_chain_view_middle"
36+
app:layout_constraintHorizontal_weight="10"
37+
app:layout_constraintStart_toStartOf="parent"
38+
app:layout_constraintTop_toTopOf="parent" />
39+
40+
<View
41+
android:id="@+id/view_chain_view_middle"
42+
style="@style/MediumBox.Purple"
43+
android:layout_width="0dp"
44+
45+
app:layout_constraintEnd_toStartOf="@+id/view_chain_view_last"
46+
app:layout_constraintHorizontal_weight="60"
47+
app:layout_constraintStart_toEndOf="@+id/view_chain_view_first"
48+
app:layout_constraintTop_toTopOf="parent" />
49+
50+
<View
51+
android:id="@+id/view_chain_view_last"
52+
style="@style/MediumBox.Variant1"
53+
android:layout_width="0dp"
54+
app:layout_constraintEnd_toEndOf="parent"
55+
app:layout_constraintHorizontal_weight="30"
56+
app:layout_constraintStart_toEndOf="@+id/view_chain_view_middle"
57+
app:layout_constraintTop_toTopOf="parent" />
58+
59+
60+
<!--
61+
Additional text and actions to explain the views - ignore below
62+
-->
63+
<TextView
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_marginTop="24dp"
67+
android:gravity="center_horizontal"
68+
android:text="
69+
Chained views with weight \n
70+
71+
From left, weight is set to\n
72+
layout_constraintHorizontal_weight='10',\n
73+
layout_constraintHorizontal_weight='60', and\n
74+
layout_constraintHorizontal_weight='30' respectively."
75+
app:layout_constraintEnd_toEndOf="parent"
76+
app:layout_constraintStart_toStartOf="parent"
77+
app:layout_constraintTop_toBottomOf="@+id/view_chain_view_middle" />
78+
79+
80+
</android.support.constraint.ConstraintLayout>

app/src/main/res/values/styles.xml

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
<item name="android:background">@color/md_amber_700</item>
2929
</style>
3030

31+
<style name="MediumBox.Variant1">
32+
<item name="android:background">@color/md_red_700</item>
33+
</style>
34+
3135
<style name="MediumBox.Purple">
3236
<item name="android:layout_width">@dimen/box_size_medium</item>
3337
<item name="android:layout_height">@dimen/box_size_medium</item>

0 commit comments

Comments
 (0)