提交 9e16a8de 编写于 作者: T Takeshi Hagikura

Adds settings for default values for the new flex items added through the

demo app UI.

Fixes some issues.
- Remove the not used test class (ExampleUnitTest).
- Adds a condition to prevent infinite loops to FlexboxLayout#shrinkFlexItems

Change-Id: Id5c375944e1d4d8f651c27efef16b24b87b38e5b
上级 7185b74e
......@@ -33,6 +33,8 @@ limitations under the License.
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity" />
</application>
</manifest>
......@@ -16,6 +16,11 @@
package com.google.android.apps.flexbox;
import com.google.android.apps.flexbox.validators.FlexBasisPercentInputValidator;
import com.google.android.apps.flexbox.validators.InputValidator;
import com.google.android.apps.flexbox.validators.IntegerInputValidator;
import com.google.android.apps.flexbox.validators.NonNegativeIntegerInputValidator;
import com.google.android.apps.flexbox.validators.DimensionInputValidator;
import com.google.android.libraries.flexbox.FlexboxLayout;
import android.app.Activity;
......@@ -72,11 +77,7 @@ public class FlexItemEditFragment extends DialogFragment {
}
Bundle args = getArguments();
mFlexItem = args.getParcelable(FLEX_ITEM_KEY);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Activity activity = getActivity();
ALIGN_SELF_AUTO = activity.getString(R.string.auto);
ALIGN_SELF_FLEX_START = activity.getString(R.string.flex_start);
......@@ -98,7 +99,7 @@ public class FlexItemEditFragment extends DialogFragment {
EditText orderEdit = (EditText) view.findViewById(R.id.edit_text_order);
orderEdit.setText(String.valueOf(mFlexItem.order));
orderEdit.addTextChangedListener(
new FlexEditTextWatcher(orderTextInput, new IntegerInputVerifier(),
new FlexEditTextWatcher(orderTextInput, new IntegerInputValidator(),
R.string.must_be_integer));
final TextInputLayout flexGrowInput = (TextInputLayout) view
......@@ -106,7 +107,7 @@ public class FlexItemEditFragment extends DialogFragment {
EditText flexGrowEdit = (EditText) view.findViewById(R.id.edit_text_flex_grow);
flexGrowEdit.setText(String.valueOf(mFlexItem.flexGrow));
flexGrowEdit.addTextChangedListener(
new FlexEditTextWatcher(flexGrowInput, new NonNegativeIntegerInputVerifier(),
new FlexEditTextWatcher(flexGrowInput, new NonNegativeIntegerInputValidator(),
R.string.must_be_non_negative_integer));
final TextInputLayout flexShrinkInput = (TextInputLayout) view
......@@ -115,7 +116,7 @@ public class FlexItemEditFragment extends DialogFragment {
R.id.edit_text_flex_shrink);
flexShrinkEdit.setText(String.valueOf(mFlexItem.flexShrink));
flexShrinkEdit.addTextChangedListener(
new FlexEditTextWatcher(flexShrinkInput, new NonNegativeIntegerInputVerifier(),
new FlexEditTextWatcher(flexShrinkInput, new NonNegativeIntegerInputValidator(),
R.string.must_be_non_negative_integer));
final TextInputLayout flexBasisPercentInput = (TextInputLayout) view
......@@ -129,7 +130,7 @@ public class FlexItemEditFragment extends DialogFragment {
flexBasisPercentEdit.setText(String.valueOf((int) mFlexItem.flexBasisPercent));
}
flexBasisPercentEdit.addTextChangedListener(
new FlexEditTextWatcher(flexBasisPercentInput, new FlexBasisPercentInputVerifier(),
new FlexEditTextWatcher(flexBasisPercentInput, new FlexBasisPercentInputValidator(),
R.string.must_be_minus_one_or_non_negative_integer));
final TextInputLayout widthInput = (TextInputLayout) view
......@@ -137,7 +138,7 @@ public class FlexItemEditFragment extends DialogFragment {
EditText widthEdit = (EditText) view.findViewById(R.id.edit_text_width);
widthEdit.setText(String.valueOf(mFlexItem.width));
widthEdit.addTextChangedListener(
new FlexEditTextWatcher(widthInput, new SizeUnitInputVerifier(),
new FlexEditTextWatcher(widthInput, new DimensionInputValidator(),
R.string.must_be_minus_one_or_minus_two_or_non_negative_integer));
final TextInputLayout heightInput = (TextInputLayout) view
......@@ -146,7 +147,7 @@ public class FlexItemEditFragment extends DialogFragment {
R.id.edit_text_height);
heightEdit.setText(String.valueOf(mFlexItem.height));
heightEdit.addTextChangedListener(
new FlexEditTextWatcher(heightInput, new SizeUnitInputVerifier(),
new FlexEditTextWatcher(heightInput, new DimensionInputValidator(),
R.string.must_be_minus_one_or_minus_two_or_non_negative_integer));
Spinner alignSelfSpinner = (Spinner) view.findViewById(
......@@ -233,10 +234,10 @@ public class FlexItemEditFragment extends DialogFragment {
private class FlexEditTextWatcher implements TextWatcher {
TextInputLayout mTextInputLayout;
InputVerifier mInputVerifier;
InputValidator mInputVerifier;
int mErrorMessageId;
FlexEditTextWatcher(TextInputLayout textInputLayout, InputVerifier inputVerifier,
FlexEditTextWatcher(TextInputLayout textInputLayout, InputValidator inputVerifier,
int errorMessageId) {
mTextInputLayout = textInputLayout;
mInputVerifier = inputVerifier;
......@@ -294,55 +295,6 @@ public class FlexItemEditFragment extends DialogFragment {
}
}
/**
* Verifies the input in a EditText
*/
private interface InputVerifier {
boolean isValidInput(CharSequence charSequence);
}
private static class IntegerInputVerifier implements InputVerifier {
@Override
public boolean isValidInput(CharSequence charSequence) {
try {
Integer.parseInt(charSequence.toString());
} catch (NumberFormatException | NullPointerException e) {
return false;
}
return true;
}
}
private static class NonNegativeIntegerInputVerifier implements InputVerifier {
@Override
public boolean isValidInput(CharSequence charSequence) {
return !TextUtils.isEmpty(charSequence) && TextUtils.isDigitsOnly(charSequence)
&& Integer.valueOf(charSequence.toString()) >= 0;
}
}
private static class SizeUnitInputVerifier implements InputVerifier {
@Override
public boolean isValidInput(CharSequence charSequence) {
// -1 represents match_parent, -2 represents wrap_content
return !TextUtils.isEmpty(charSequence) &&
(TextUtils.isDigitsOnly(charSequence) ||
charSequence.toString().equals("-1") ||
charSequence.toString().equals("-2"));
}
}
private static class FlexBasisPercentInputVerifier implements InputVerifier {
@Override
public boolean isValidInput(CharSequence charSequence) {
// -1 represents not set
return !TextUtils.isEmpty(charSequence) &&
(TextUtils.isDigitsOnly(charSequence) ||
charSequence.toString().equals("-1"));
}
}
/**
* A listener that listens to the change of a flex item
*/
......
......@@ -18,9 +18,10 @@ package com.google.android.apps.flexbox;
import com.google.android.libraries.flexbox.FlexboxLayout;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.MenuItemCompat;
......@@ -38,7 +39,6 @@ import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.security.SecureRandom;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
......@@ -46,6 +46,8 @@ public class MainActivity extends AppCompatActivity
private static final String FLEX_ITEMS_KEY = "flex_items";
private static final String EDIT_DIALOG_TAG = "edit_dialog_tag";
private static final String DEFAULT_WIDTH = "120";
private static final String DEFAULT_HEIGHT = "80";
private String ROW;
private String COLUMN;
......@@ -63,6 +65,7 @@ public class MainActivity extends AppCompatActivity
private String SPACE_AROUND;
private FlexboxLayout mFlexboxLayout;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -71,6 +74,7 @@ public class MainActivity extends AppCompatActivity
initializeStringResrouces();
mFlexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
......@@ -122,12 +126,7 @@ public class MainActivity extends AppCompatActivity
// index starts from 0. New View's index is N if N views ([0, 1, 2, ... N-1])
// exist.
TextView textView = createBaseFlexItemTextView(viewIndex);
int height = (int) getResources().getDimension(
R.dimen.flex_item_length);
int width = getRandomFlexItemWidth(getResources(), height);
FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(
width, height);
textView.setLayoutParams(lp);
textView.setLayoutParams(createDefaultLayoutParams());
textView.setOnClickListener(new FlexItemClickListener(viewIndex));
mFlexboxLayout.addView(textView);
}
......@@ -190,13 +189,33 @@ public class MainActivity extends AppCompatActivity
return textView;
}
private int getRandomFlexItemWidth(Resources resources, float defaultValue) {
SecureRandom random = new SecureRandom();
TypedArray candidates = resources.obtainTypedArray(R.array.flex_item_width_candidates);
int width = (int) candidates
.getDimension(random.nextInt(candidates.length()), defaultValue);
candidates.recycle();
return width;
/**
* Creates a new {@link FlexboxLayout.LayoutParams} based on the stored default values in
* the SharedPreferences.
*
* @return a {@link FlexboxLayout.LayoutParams} instance
*/
private FlexboxLayout.LayoutParams createDefaultLayoutParams() {
FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(
Util.dpToPixel(this,
readPreferenceAsInteger(getString(R.string.new_width_key), DEFAULT_WIDTH)),
Util.dpToPixel(this, readPreferenceAsInteger(getString(R.string.new_height_key),
DEFAULT_HEIGHT)));
lp.order = readPreferenceAsInteger(getString(R.string.new_flex_item_order_key), "1");
lp.flexGrow = readPreferenceAsInteger(getString(R.string.new_flex_grow_key), "0");
lp.flexShrink = readPreferenceAsInteger(getString(R.string.new_flex_shrink_key), "1");
int flexBasisPercent = readPreferenceAsInteger(
getString(R.string.new_flex_basis_percent_key), "-1");
lp.flexBasisPercent = flexBasisPercent == -1 ? -1 : (float) (flexBasisPercent / 100.0);
return lp;
}
private int readPreferenceAsInteger(String key, String defValue) {
if (mSharedPreferences.contains(key)) {
return Integer.valueOf(mSharedPreferences.getString(key, defValue));
} else {
return Integer.valueOf(defValue);
}
}
private void initializeSpinner(int currentValue, int menuItemId, Menu navigationMenu,
......@@ -213,7 +232,6 @@ public class MainActivity extends AppCompatActivity
spinner.setSelection(position);
}
private void initializeFlexDirectionSpinner(Menu navigationMenu) {
initializeSpinner(mFlexboxLayout.getFlexDirection(), R.id.menu_item_flex_direction,
navigationMenu, R.array.array_flex_direction,
......@@ -446,6 +464,24 @@ public class MainActivity extends AppCompatActivity
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private class FlexItemClickListener implements View.OnClickListener {
int mViewIndex;
......
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox;
import com.google.android.apps.flexbox.validators.FlexBasisPercentInputValidator;
import com.google.android.apps.flexbox.validators.InputValidator;
import com.google.android.apps.flexbox.validators.DimensionInputValidator;
import android.app.Activity;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.widget.Toast;
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
}
/**
* Fragment for settings.
*/
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.new_flex_item_preferences);
EditTextPreference flexBasisPercentPreference = (EditTextPreference) findPreference(
getString(R.string.new_flex_basis_percent_key));
flexBasisPercentPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
InputValidator validator = new FlexBasisPercentInputValidator();
if (!validator.isValidInput(newValue.toString())) {
Toast.makeText(getActivity(),
R.string.must_be_minus_one_or_non_negative_integer,
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
});
EditTextPreference widthPreference = (EditTextPreference) findPreference(
getString(R.string.new_width_key));
widthPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
InputValidator validator = new DimensionInputValidator();
if (!validator.isValidInput(newValue.toString())) {
Toast.makeText(getActivity(),
R.string.must_be_minus_one_or_minus_two_or_non_negative_integer,
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
});
EditTextPreference heightPreference = (EditTextPreference) findPreference(
getString(R.string.new_height_key));
heightPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
InputValidator validator = new DimensionInputValidator();
if (!validator.isValidInput(newValue.toString())) {
Toast.makeText(getActivity(),
R.string.must_be_minus_one_or_minus_two_or_non_negative_integer,
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
});
}
}
}
......@@ -35,7 +35,7 @@ public class Util {
*/
public static int pixelToDp(Context context, int pixel) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return pixel < 0 ? pixel : (int) (pixel / displayMetrics.density);
return pixel < 0 ? pixel : Math.round(pixel / displayMetrics.density);
}
/**
......@@ -48,6 +48,6 @@ public class Util {
*/
public static int dpToPixel(Context context, int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return dp < 0 ? dp : (int) (dp * displayMetrics.density);
return dp < 0 ? dp : Math.round(dp * displayMetrics.density);
}
}
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox.validators;
import android.text.TextUtils;
/**
* Validator for dimension values.
*/
public class DimensionInputValidator implements InputValidator {
@Override
public boolean isValidInput(CharSequence charSequence) {
// -1 represents match_parent, -2 represents wrap_content
return !TextUtils.isEmpty(charSequence) &&
(TextUtils.isDigitsOnly(charSequence) ||
charSequence.toString().equals("-1") ||
charSequence.toString().equals("-2"));
}
}
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox.validators;
import android.text.TextUtils;
/**
* Validator for the flex basis percent attribute.
*/
public class FlexBasisPercentInputValidator implements InputValidator {
@Override
public boolean isValidInput(CharSequence charSequence) {
// -1 represents not set
return !TextUtils.isEmpty(charSequence) &&
(TextUtils.isDigitsOnly(charSequence) ||
charSequence.toString().equals("-1"));
}
}
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox.validators;
/**
* Interface to verify a given input.
*/
public interface InputValidator {
/**
* Verifies if the given input is valid.
*
* @param charSequence the input to be verified
* @return {@code true} if charSequence is valid, {@code false} otherwise
*/
boolean isValidInput(CharSequence charSequence);
}
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox.validators;
/**
* Validator for the integers.
*/
public class IntegerInputValidator implements InputValidator {
@Override
public boolean isValidInput(CharSequence charSequence) {
try {
Integer.parseInt(charSequence.toString());
} catch (NumberFormatException | NullPointerException e) {
return false;
}
return true;
}
}
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.flexbox.validators;
import android.text.TextUtils;
/**
* Validator for non negative integers.
*/
public class NonNegativeIntegerInputValidator implements InputValidator {
@Override
public boolean isValidInput(CharSequence charSequence) {
return !TextUtils.isEmpty(charSequence) && TextUtils.isDigitsOnly(charSequence)
&& Integer.valueOf(charSequence.toString()) >= 0;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
\ No newline at end of file
......@@ -34,10 +34,4 @@ limitations under the License.
<dimen name="flex_item_length">80dp</dimen>
<dimen name="flex_item_length2">120dp</dimen>
<dimen name="flex_item_length3">160dp</dimen>
<array name="flex_item_width_candidates">
<item>@dimen/flex_item_length</item>
<item>@dimen/flex_item_length2</item>
<item>@dimen/flex_item_length3</item>
</array>
</resources>
......@@ -107,4 +107,19 @@ limitations under the License.
<string name="must_be_integer">Must be an integer value</string>
<string name="must_be_minus_one_or_non_negative_integer">Must be -1 or a non-negative integer value</string>
<string name="invalid_values_exist">Invalid values exist</string>
<string name="action_settings">Settings</string>
<string name="new_flex_items_default">Default values for new flex items</string>
<string name="new_flex_items_default_key" translatable="false">new_flex_items_default_key</string>
<string name="new_flex_item_order_key" translatable="false">new_flex_item_order_key</string>
<string name="new_flex_grow_key" translatable="false">new_flex_grow_key</string>
<string name="new_flex_shrink_key" translatable="false">new_flex_shrink_key</string>
<string name="new_flex_basis_percent_key" translatable="false">new_flex_basis_percent_key</string>
<string name="flex_basis_percent" translatable="false">Flex Basis Percent</string>
<string name="flex_basis_percent_summary">(-1: not set)</string>
<string name="width">Width</string>
<string name="height">Height</string>
<string name="size_unit_summary">(-1: match_parent, -2: wrap_content)</string>
<string name="new_width_key" translatable="false">new_width_key</string>
<string name="new_height_key" translatable="false">new_height_key</string>
</resources>
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/new_flex_items_default"
android:key="@string/new_flex_items_default_key" >
<EditTextPreference
android:key="@string/new_flex_item_order_key"
android:title="@string/hint_order"
android:inputType="numberSigned"
android:persistent="true"
android:defaultValue="1" />
<EditTextPreference
android:key="@string/new_flex_grow_key"
android:title="@string/hint_flex_grow"
android:inputType="number"
android:persistent="true"
android:defaultValue="0" />
<EditTextPreference
android:key="@string/new_flex_shrink_key"
android:title="@string/hint_flex_shrink"
android:inputType="number"
android:persistent="true"
android:defaultValue="1" />
<EditTextPreference
android:key="@string/new_flex_basis_percent_key"
android:title="@string/flex_basis_percent"
android:summary="@string/flex_basis_percent_summary"
android:inputType="numberSigned"
android:persistent="true"
android:defaultValue="-1" />
<EditTextPreference
android:key="@string/new_width_key"
android:title="@string/width"
android:summary="@string/size_unit_summary"
android:inputType="numberSigned"
android:persistent="true"
android:defaultValue="120" />
<EditTextPreference
android:key="@string/new_height_key"
android:title="@string/height"
android:summary="@string/size_unit_summary"
android:inputType="numberSigned"
android:persistent="true"
android:defaultValue="80" />
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
......@@ -602,6 +602,7 @@ public class FlexboxLayout extends ViewGroup {
private int shrinkFlexItems(FlexLine flexLine, @FlexDirection int flexDirection,
int maxMainSize, int paddingAlongMainAxis, int startIndex) {
int childIndex = startIndex;
int sizeBeforeShrink = flexLine.mainSize;
if (flexLine.totalFlexShrink <= 0 || maxMainSize > flexLine.mainSize) {
childIndex += flexLine.itemCount;
return childIndex;
......@@ -649,7 +650,9 @@ public class FlexboxLayout extends ViewGroup {
childIndex++;
}
if (needsReshrink) {
if (needsReshrink && sizeBeforeShrink != flexLine.mainSize) {
// Re-invoke the method with the same startIndex to distribute the negative free space
// that wasn't fully distributed (because some views length were not enough)
shrinkFlexItems(flexLine, flexDirection, maxMainSize, paddingAlongMainAxis, startIndex);
}
return childIndex;
......
package com.google.android.libraries.flexbox;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册