Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 8b0b6c7

Browse files
tomashavlicekthagikura
authored andcommitted
Fix aspect ratio landscape (#45)
* Add support for layout-land resources * Adjusted constrain_example_5 to better fit land mode
1 parent a0bf21e commit 8b0b6c7

File tree

2 files changed

+105
-9
lines changed

2 files changed

+105
-9
lines changed

constraintlayout/src/main/java/com/example/android/constraintlayoutexamples/MainActivity.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,36 @@
1717
package com.example.android.constraintlayoutexamples;
1818

1919
import android.content.Intent;
20+
import android.content.res.Configuration;
2021
import android.os.Bundle;
2122
import android.support.v7.app.AppCompatActivity;
2223
import android.view.View;
2324

2425
public class MainActivity extends AppCompatActivity {
25-
private boolean mShowingLayout = false;
26+
private String mTag = "activity_main";
2627

2728
@Override
2829
protected void onCreate(Bundle savedInstanceState) {
2930
super.onCreate(savedInstanceState);
30-
setContentView(R.layout.activity_main);
31+
setContentView(mTag);
32+
}
33+
34+
@Override
35+
public void onConfigurationChanged(Configuration newConfig) {
36+
super.onConfigurationChanged(newConfig);
37+
setContentView(mTag);
3138
}
3239

3340
public void show(View v) {
34-
String tag = (String) v.getTag();
35-
int id = getResources().getIdentifier(tag, "layout", getPackageName());
36-
setContentView(id);
37-
mShowingLayout = true;
41+
mTag = (String) v.getTag();
42+
setContentView(mTag);
3843
}
3944

4045
@Override
4146
public void onBackPressed() {
42-
if (mShowingLayout) {
43-
setContentView(R.layout.activity_main);
44-
mShowingLayout = false;
47+
if (!mTag.equals("activity_main")) {
48+
mTag = "activity_main";
49+
setContentView(mTag);
4550
} else {
4651
super.onBackPressed();
4752
}
@@ -50,4 +55,9 @@ public void onBackPressed() {
5055
public void showConstraintSetExample(View view) {
5156
startActivity(new Intent(this, ConstraintSetExampleActivity.class));
5257
}
58+
59+
private void setContentView(String tag) {
60+
int id = getResources().getIdentifier(tag, "layout", getPackageName());
61+
setContentView(id);
62+
}
5363
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2017 The Android Open Source Project
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
15+
xmlns:app="http://schemas.android.com/apk/res-auto"
16+
xmlns:tools="http://schemas.android.com/tools"
17+
android:orientation="vertical"
18+
android:layout_width="match_parent"
19+
android:layout_height="match_parent">
20+
21+
<TextView
22+
android:id="@+id/textView3"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_marginTop="16dp"
26+
android:text="@string/aspect_ratio"
27+
android:textSize="30sp"
28+
app:layout_constraintLeft_toLeftOf="parent"
29+
app:layout_constraintRight_toRightOf="parent"
30+
app:layout_constraintTop_toTopOf="parent"
31+
tools:layout_constraintLeft_creator="1"
32+
tools:layout_constraintRight_creator="1"
33+
tools:layout_constraintTop_creator="1" />
34+
35+
<TextView
36+
android:id="@+id/textView4"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_marginTop="24dp"
40+
android:text="@string/big_android_icon"
41+
android:textSize="24sp"
42+
app:layout_constraintLeft_toRightOf="@+id/imageButton"
43+
app:layout_constraintTop_toBottomOf="@+id/imageView"
44+
tools:layout_constraintTop_creator="1" />
45+
46+
<TextView
47+
android:id="@+id/textView5"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:text="@string/textview"
51+
android:textSize="24sp"
52+
app:layout_constraintLeft_toLeftOf="@+id/textView4"
53+
app:layout_constraintTop_toBottomOf="@+id/textView4"
54+
tools:layout_constraintLeft_creator="1"
55+
tools:layout_constraintTop_creator="1" />
56+
57+
<ImageView
58+
android:id="@+id/imageView"
59+
android:layout_width="0dp"
60+
android:layout_height="0dp"
61+
android:layout_marginLeft="16dp"
62+
android:layout_marginTop="16dp"
63+
android:layout_marginRight="16dp"
64+
android:contentDescription="@string/lake_tahoe_image"
65+
android:scaleType="centerCrop"
66+
app:layout_constraintDimensionRatio="h,6:1"
67+
app:layout_constraintLeft_toLeftOf="parent"
68+
app:layout_constraintRight_toRightOf="parent"
69+
app:layout_constraintTop_toBottomOf="@+id/textView3"
70+
app:srcCompat="@drawable/lake"
71+
tools:layout_constraintLeft_creator="1"
72+
tools:layout_constraintRight_creator="1" />
73+
74+
<ImageButton
75+
android:id="@+id/imageButton"
76+
android:layout_width="0dp"
77+
android:layout_height="0dp"
78+
app:srcCompat="@drawable/ic_android_black_24dp"
79+
tools:layout_constraintBottom_creator="1"
80+
app:layout_constraintBottom_toBottomOf="@+id/textView5"
81+
app:layout_constraintTop_toTopOf="@+id/textView4"
82+
android:layout_marginStart="16dp"
83+
app:layout_constraintLeft_toLeftOf="parent"
84+
app:layout_constraintDimensionRatio="w,1:1"
85+
android:contentDescription="@string/android_button" />
86+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)