Skip to content

Commit 027714c

Browse files
committed
增加约束布局ConstraintLayout的使用案例
1 parent 321bdf8 commit 027714c

File tree

8 files changed

+780
-0
lines changed

8 files changed

+780
-0
lines changed

app/src/main/java/com/xuexiang/xuidemo/fragment/expands/MaterialDesignFragment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.xuexiang.xuidemo.base.BaseSimpleListFragment;
77
import com.xuexiang.xuidemo.fragment.expands.materialdesign.BehaviorFragment;
88
import com.xuexiang.xuidemo.fragment.expands.materialdesign.BottomSheetDialogFragment;
9+
import com.xuexiang.xuidemo.fragment.expands.materialdesign.ConstraintLayoutFragment;
910
import com.xuexiang.xuidemo.fragment.expands.materialdesign.DrawerLayoutFragment;
1011
import com.xuexiang.xuidemo.fragment.expands.materialdesign.ToolBarFragment;
1112
import com.xuexiang.xutil.app.ActivityUtils;
@@ -32,6 +33,7 @@ protected List<String> initSimpleData(List<String> lists) {
3233
lists.add("Behavior\n手势行为");
3334
lists.add("DrawerLayout + NavigationView\n常见主页布局");
3435
lists.add("BottomSheetDialog");
36+
lists.add("ConstraintLayout\n约束布局");
3537
lists.add("设置页面");
3638
return lists;
3739
}
@@ -57,6 +59,9 @@ protected void onItemClick(int position) {
5759
openPage(BottomSheetDialogFragment.class);
5860
break;
5961
case 4:
62+
openPage(ConstraintLayoutFragment.class);
63+
break;
64+
case 5:
6065
ActivityUtils.startActivity(SettingsActivity.class);
6166
break;
6267
default:
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.xuexiang.xuidemo.fragment.expands.materialdesign;
19+
20+
import com.xuexiang.xpage.annotation.Page;
21+
import com.xuexiang.xpage.core.PageOption;
22+
import com.xuexiang.xuidemo.R;
23+
import com.xuexiang.xuidemo.base.BaseSimpleListFragment;
24+
import com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout.ConstraintLayoutContainerFragment;
25+
26+
import java.util.List;
27+
28+
import static com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout.ConstraintLayoutContainerFragment.KEY_LAYOUT_ID;
29+
import static com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout.ConstraintLayoutContainerFragment.KEY_TITLE;
30+
31+
/**
32+
* @author xuexiang
33+
* @since 2020-01-08 9:34
34+
*/
35+
@Page(name = "ConstraintLayout\n约束布局")
36+
public class ConstraintLayoutFragment extends BaseSimpleListFragment {
37+
38+
@Override
39+
protected List<String> initSimpleData(List<String> lists) {
40+
lists.add("对齐方式");
41+
lists.add("尺寸约束");
42+
lists.add("链约束");
43+
lists.add("导航线Guideline使用");
44+
return lists;
45+
}
46+
47+
@Override
48+
protected void onItemClick(int position) {
49+
switch (position) {
50+
case 0:
51+
gotoExample(mSimpleData.get(position), R.layout.layout_constraint_align);
52+
break;
53+
case 1:
54+
gotoExample(mSimpleData.get(position), R.layout.layout_constraint_size);
55+
break;
56+
case 2:
57+
gotoExample(mSimpleData.get(position), R.layout.layout_constraint_chain);
58+
break;
59+
case 3:
60+
gotoExample(mSimpleData.get(position), R.layout.layout_constraint_guideline);
61+
break;
62+
default:
63+
break;
64+
}
65+
}
66+
67+
68+
private void gotoExample(String title, int layoutId) {
69+
PageOption.to(ConstraintLayoutContainerFragment.class)
70+
.putString(KEY_TITLE, title)
71+
.putInt(KEY_LAYOUT_ID, layoutId)
72+
.open(this);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout;
19+
20+
import android.os.Bundle;
21+
import android.view.LayoutInflater;
22+
import android.view.View;
23+
import android.view.ViewGroup;
24+
25+
import androidx.annotation.Nullable;
26+
27+
import com.xuexiang.xpage.annotation.Page;
28+
import com.xuexiang.xrouter.annotation.AutoWired;
29+
import com.xuexiang.xrouter.launcher.XRouter;
30+
import com.xuexiang.xui.widget.actionbar.TitleBar;
31+
import com.xuexiang.xuidemo.base.BaseFragment;
32+
33+
import static com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout.ConstraintLayoutContainerFragment.KEY_LAYOUT_ID;
34+
import static com.xuexiang.xuidemo.fragment.expands.materialdesign.constraintlayout.ConstraintLayoutContainerFragment.KEY_TITLE;
35+
36+
/**
37+
* @author xuexiang
38+
* @since 2020-01-08 10:19
39+
*/
40+
@Page(params = {KEY_TITLE, KEY_LAYOUT_ID})
41+
public class ConstraintLayoutContainerFragment extends BaseFragment {
42+
public static final String KEY_TITLE = "key_title";
43+
public static final String KEY_LAYOUT_ID = "key_layout_id";
44+
@AutoWired(name = KEY_TITLE)
45+
String title;
46+
@AutoWired(name = KEY_LAYOUT_ID)
47+
int layoutId;
48+
49+
@Nullable
50+
@Override
51+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
52+
XRouter.getInstance().inject(this);
53+
return super.onCreateView(inflater, container, savedInstanceState);
54+
}
55+
56+
@Override
57+
protected TitleBar initTitle() {
58+
return super.initTitle().setTitle(title);
59+
}
60+
61+
@Override
62+
protected int getLayoutId() {
63+
return layoutId;
64+
}
65+
66+
@Override
67+
protected void initViews() {
68+
69+
}
70+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
~
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:orientation="vertical">
23+
24+
<androidx.constraintlayout.widget.ConstraintLayout
25+
android:layout_width="match_parent"
26+
android:layout_height="match_parent">
27+
28+
<TextView
29+
android:id="@+id/text4"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:background="@color/app_color_theme_1"
33+
android:text="顶部水平居中"
34+
android:textColor="@android:color/white"
35+
app:layout_constraintEnd_toEndOf="parent"
36+
app:layout_constraintStart_toStartOf="parent"
37+
app:layout_constraintTop_toTopOf="parent" />
38+
39+
<TextView
40+
android:id="@+id/text5"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:background="@color/app_color_theme_1"
44+
android:text="底部水平居中"
45+
android:textColor="@android:color/white"
46+
app:layout_constraintBottom_toBottomOf="parent"
47+
app:layout_constraintEnd_toEndOf="parent"
48+
app:layout_constraintStart_toStartOf="parent" />
49+
50+
<TextView
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:layout_marginTop="@dimen/spacing_16"
54+
android:background="@color/app_color_theme_5"
55+
android:text="水平方向1/3处"
56+
android:textColor="@android:color/white"
57+
app:layout_constraintEnd_toEndOf="parent"
58+
app:layout_constraintHorizontal_bias="0.33"
59+
app:layout_constraintStart_toStartOf="parent"
60+
app:layout_constraintTop_toBottomOf="@id/text4" />
61+
62+
<TextView
63+
android:layout_width="wrap_content"
64+
android:layout_height="wrap_content"
65+
android:background="@color/app_color_theme_2"
66+
android:text="左端垂直居中"
67+
android:textColor="@android:color/white"
68+
app:layout_constraintBottom_toBottomOf="parent"
69+
app:layout_constraintStart_toStartOf="parent"
70+
app:layout_constraintTop_toTopOf="parent" />
71+
72+
<TextView
73+
android:layout_width="wrap_content"
74+
android:layout_height="wrap_content"
75+
android:background="@color/app_color_theme_2"
76+
android:text="右端垂直居中"
77+
android:textColor="@android:color/white"
78+
app:layout_constraintBottom_toBottomOf="parent"
79+
app:layout_constraintEnd_toEndOf="parent"
80+
app:layout_constraintTop_toTopOf="parent" />
81+
82+
<TextView
83+
android:layout_width="wrap_content"
84+
android:layout_height="wrap_content"
85+
android:background="@color/app_color_theme_5"
86+
android:text="垂直方向1/3处"
87+
android:textColor="@android:color/white"
88+
app:layout_constraintBottom_toBottomOf="parent"
89+
app:layout_constraintStart_toStartOf="parent"
90+
app:layout_constraintTop_toTopOf="parent"
91+
app:layout_constraintVertical_bias="0.33" />
92+
93+
<TextView
94+
android:id="@+id/text1"
95+
android:layout_width="wrap_content"
96+
android:layout_height="wrap_content"
97+
android:background="@color/app_color_theme_3"
98+
android:text="text1:全局居中"
99+
android:textColor="@android:color/white"
100+
app:layout_constraintBottom_toBottomOf="parent"
101+
app:layout_constraintEnd_toEndOf="parent"
102+
app:layout_constraintStart_toStartOf="parent"
103+
app:layout_constraintTop_toTopOf="parent" />
104+
105+
<TextView
106+
android:id="@+id/text2"
107+
android:layout_width="wrap_content"
108+
android:layout_height="wrap_content"
109+
android:layout_marginBottom="@dimen/spacing_16"
110+
android:background="@color/app_color_theme_4"
111+
android:text="text2:位于text1上,右端对齐"
112+
android:textColor="@android:color/white"
113+
app:layout_constraintBottom_toTopOf="@id/text1"
114+
app:layout_constraintEnd_toEndOf="@id/text1" />
115+
116+
<TextView
117+
android:id="@+id/text3"
118+
android:layout_width="wrap_content"
119+
android:layout_height="wrap_content"
120+
android:layout_marginTop="@dimen/spacing_16"
121+
android:background="@color/app_color_theme_4"
122+
android:text="text3:位于text1下,左端对齐"
123+
android:textColor="@android:color/white"
124+
app:layout_constraintStart_toStartOf="@id/text1"
125+
app:layout_constraintTop_toBottomOf="@id/text1" />
126+
127+
128+
<TextView
129+
android:layout_width="wrap_content"
130+
android:layout_height="wrap_content"
131+
android:background="@color/app_color_theme_8"
132+
android:text="位于text1 225°,150dp"
133+
android:textColor="@android:color/white"
134+
app:layout_constraintCircle="@+id/text1"
135+
app:layout_constraintCircleAngle="225"
136+
app:layout_constraintCircleRadius="150dp"
137+
app:layout_constraintStart_toStartOf="parent"
138+
app:layout_constraintTop_toTopOf="parent" />
139+
140+
<TextView
141+
android:layout_width="wrap_content"
142+
android:layout_height="wrap_content"
143+
android:background="@color/app_color_theme_8"
144+
android:text="位于text1 135°,130dp"
145+
android:textColor="@android:color/white"
146+
app:layout_constraintCircle="@+id/text1"
147+
app:layout_constraintCircleAngle="135"
148+
app:layout_constraintCircleRadius="130dp"
149+
app:layout_constraintStart_toStartOf="parent"
150+
app:layout_constraintTop_toTopOf="parent" />
151+
152+
<TextView
153+
android:id="@+id/text6"
154+
android:layout_width="wrap_content"
155+
android:layout_height="wrap_content"
156+
android:layout_marginBottom="@dimen/spacing_16"
157+
android:background="@color/app_color_theme_6"
158+
android:text="文字对齐目标"
159+
android:textColor="@android:color/white"
160+
android:textSize="30sp"
161+
app:layout_constraintBottom_toTopOf="@id/text5"
162+
app:layout_constraintEnd_toEndOf="parent"
163+
app:layout_constraintStart_toStartOf="parent" />
164+
165+
<TextView
166+
android:layout_width="wrap_content"
167+
android:layout_height="wrap_content"
168+
android:layout_marginBottom="@dimen/spacing_16"
169+
android:background="@color/app_color_theme_7"
170+
android:text="基线对齐"
171+
android:textColor="@android:color/white"
172+
app:layout_constraintBaseline_toBaselineOf="@id/text6"
173+
app:layout_constraintBottom_toTopOf="@id/text5"
174+
app:layout_constraintEnd_toStartOf="@id/text6" />
175+
176+
<TextView
177+
android:layout_width="wrap_content"
178+
android:layout_height="wrap_content"
179+
android:layout_marginBottom="@dimen/spacing_16"
180+
android:background="@color/app_color_theme_7"
181+
android:text="基线不对齐"
182+
android:textColor="@android:color/white"
183+
app:layout_constraintBottom_toTopOf="@id/text5"
184+
app:layout_constraintStart_toEndOf="@id/text6" />
185+
186+
</androidx.constraintlayout.widget.ConstraintLayout>
187+
188+
</LinearLayout>

0 commit comments

Comments
 (0)