Skip to content

Commit 331bb0c

Browse files
author
Xavier Ducrohet
committed
Setup ActionBars in layoutlib the same way the platform does it. do not merge.
Instead of using a simple ImageView for the icon, this uses the platform layout/action_bar_home which uses a custom class to position and resize the icon (and also supports the Up icon that we don't yet support). This ensures that the icon is properly positionned and sized like on devices. (cherry picked from commit 7396348) Change-Id: Ifd3bc318089b70ba843519523e366e59d434e919
1 parent a9d9fa7 commit 331bb0c

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<merge xmlns:android="http://schemas.android.com/apk/res/android">
3-
<ImageView
4-
android:layout_height="wrap_content"
5-
android:layout_width="wrap_content"/>
6-
<TextView
7-
android:layout_width="wrap_content"
8-
android:layout_height="wrap_content"/>
3+
<include layout="@android:layout/action_bar_home" />
4+
<TextView
5+
android:layout_width="wrap_content"
6+
android:layout_height="wrap_content"/>
97
</merge>

tools/layoutlib/bridge/src/android/graphics/Matrix_Delegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public boolean hasPerspective() {
474474
}
475475

476476
Matrix_Delegate other = sManager.getDelegate(other_matrix);
477-
if (d == null) {
477+
if (other == null) {
478478
return false;
479479
}
480480

@@ -570,7 +570,7 @@ public boolean hasPerspective() {
570570
}
571571

572572
Matrix_Delegate other = sManager.getDelegate(other_matrix);
573-
if (d == null) {
573+
if (other == null) {
574574
return false;
575575
}
576576

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/CustomBar.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ protected void loadIcon(int index, String iconReference) {
145145
}
146146
}
147147

148+
protected void loadIconById(int id, String iconReference) {
149+
ResourceValue value = getResourceValue(iconReference);
150+
if (value != null) {
151+
loadIconById(id, value);
152+
}
153+
}
154+
155+
148156
protected Drawable loadIcon(int index, ResourceType type, String name) {
149157
BridgeContext bridgeContext = (BridgeContext) mContext;
150158
RenderResources res = bridgeContext.getRenderResources();
@@ -162,34 +170,64 @@ private Drawable loadIcon(int index, ResourceValue value) {
162170
if (child instanceof ImageView) {
163171
ImageView imageView = (ImageView) child;
164172

165-
Drawable drawable = ResourceHelper.getDrawable(
166-
value, (BridgeContext) mContext);
167-
if (drawable != null) {
168-
imageView.setBackgroundDrawable(drawable);
169-
}
173+
return loadIcon(imageView, value);
174+
}
170175

171-
return drawable;
176+
return null;
177+
}
178+
179+
private Drawable loadIconById(int id, ResourceValue value) {
180+
View child = findViewById(id);
181+
if (child instanceof ImageView) {
182+
ImageView imageView = (ImageView) child;
183+
184+
return loadIcon(imageView, value);
172185
}
173186

174187
return null;
175188
}
176189

190+
191+
private Drawable loadIcon(ImageView imageView, ResourceValue value) {
192+
Drawable drawable = ResourceHelper.getDrawable(value, (BridgeContext) mContext);
193+
if (drawable != null) {
194+
imageView.setImageDrawable(drawable);
195+
}
196+
197+
return drawable;
198+
}
199+
177200
protected TextView setText(int index, String stringReference) {
178201
View child = getChildAt(index);
179202
if (child instanceof TextView) {
180203
TextView textView = (TextView) child;
181-
ResourceValue value = getResourceValue(stringReference);
182-
if (value != null) {
183-
textView.setText(value.getValue());
184-
} else {
185-
textView.setText(stringReference);
186-
}
204+
setText(textView, stringReference);
205+
return textView;
206+
}
207+
208+
return null;
209+
}
210+
211+
protected TextView setTextById(int id, String stringReference) {
212+
View child = findViewById(id);
213+
if (child instanceof TextView) {
214+
TextView textView = (TextView) child;
215+
setText(textView, stringReference);
187216
return textView;
188217
}
189218

190219
return null;
191220
}
192221

222+
private void setText(TextView textView, String stringReference) {
223+
ResourceValue value = getResourceValue(stringReference);
224+
if (value != null) {
225+
textView.setText(value.getValue());
226+
} else {
227+
textView.setText(stringReference);
228+
}
229+
}
230+
193231
protected void setStyle(String themeEntryName) {
194232

195233
BridgeContext bridgeContext = (BridgeContext) mContext;

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/FakeActionBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public FakeActionBar(Context context, Density density, String label, String icon
3434
// Cannot access the inside items through id because no R.id values have been
3535
// created for them.
3636
// We do know the order though.
37-
loadIcon(0, icon);
37+
loadIconById(android.R.id.home, icon);
3838
mTextView = setText(1, label);
3939

4040
setStyle("actionBarStyle");

0 commit comments

Comments
 (0)