下面列出了怎么用android.support.v4.view.MarginLayoutParamsCompat的API类实例代码及写法,或者点击链接到github查看源代码。
private void addFloatingActionButton() {
final int fabSize = getResources().getDimensionPixelSize(R.dimen.size_fab);
int bottomOfQuestionView = findViewById(R.id.question_view).getBottom();
final LayoutParams fabLayoutParams = new LayoutParams(fabSize, fabSize,
Gravity.END | Gravity.TOP);
final int halfAFab = fabSize / 2;
fabLayoutParams.setMargins(0, // left
bottomOfQuestionView - halfAFab, //top
0, // right
mSpacingDouble); // bottom
MarginLayoutParamsCompat.setMarginEnd(fabLayoutParams, mSpacingDouble);
if (ApiLevelHelper.isLowerThan(Build.VERSION_CODES.LOLLIPOP)) {
// Account for the fab's emulated shadow.
fabLayoutParams.topMargin -= (mSubmitAnswer.getPaddingTop() / 2);
}
addView(mSubmitAnswer, fabLayoutParams);
}
public void restoreMarginLayoutParams(MarginLayoutParams params) {
restoreLayoutParams(params);
params.leftMargin = this.mPreservedParams.leftMargin;
params.topMargin = this.mPreservedParams.topMargin;
params.rightMargin = this.mPreservedParams.rightMargin;
params.bottomMargin = this.mPreservedParams.bottomMargin;
MarginLayoutParamsCompat.setMarginStart(params, MarginLayoutParamsCompat.getMarginStart(this.mPreservedParams));
MarginLayoutParamsCompat.setMarginEnd(params, MarginLayoutParamsCompat.getMarginEnd(this.mPreservedParams));
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
/**
* Restores the original dimensions and margins after they were changed for percentage based
* values. You should call this method only if you previously called
* {@link PercentLayoutHelper.PercentLayoutInfo#fillMarginLayoutParams(View, ViewGroup.MarginLayoutParams, int, int)}.
*/
public void restoreMarginLayoutParams(ViewGroup.MarginLayoutParams params) {
restoreLayoutParams(params);
params.leftMargin = mPreservedParams.leftMargin;
params.topMargin = mPreservedParams.topMargin;
params.rightMargin = mPreservedParams.rightMargin;
params.bottomMargin = mPreservedParams.bottomMargin;
MarginLayoutParamsCompat.setMarginStart(params,
MarginLayoutParamsCompat.getMarginStart(mPreservedParams));
MarginLayoutParamsCompat.setMarginEnd(params,
MarginLayoutParamsCompat.getMarginEnd(mPreservedParams));
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginStart(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp);
}
static int getMarginEnd(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginEnd(lp);
}
static int getMarginHorizontally(View v) {
if (v == null) {
return 0;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
/**
* Restores original dimensions and margins after they were changed for percentage based
* values. Calling this method only makes sense if you previously called
* {@link PercentLayoutHelper.PercentLayoutInfo#fillMarginLayoutParams}.
*/
public void restoreMarginLayoutParams(ViewGroup.MarginLayoutParams params)
{
restoreLayoutParams(params);
params.leftMargin = mPreservedParams.leftMargin;
params.topMargin = mPreservedParams.topMargin;
params.rightMargin = mPreservedParams.rightMargin;
params.bottomMargin = mPreservedParams.bottomMargin;
MarginLayoutParamsCompat.setMarginStart(params,
MarginLayoutParamsCompat.getMarginStart(mPreservedParams));
MarginLayoutParamsCompat.setMarginEnd(params,
MarginLayoutParamsCompat.getMarginEnd(mPreservedParams));
}
public void fillMarginLayoutParams(MarginLayoutParams params, int widthHint, int heightHint) {
int base;
fillLayoutParams(params, widthHint, heightHint);
this.mPreservedParams.leftMargin = params.leftMargin;
this.mPreservedParams.topMargin = params.topMargin;
this.mPreservedParams.rightMargin = params.rightMargin;
this.mPreservedParams.bottomMargin = params.bottomMargin;
MarginLayoutParamsCompat.setMarginStart(this.mPreservedParams, MarginLayoutParamsCompat.getMarginStart(params));
MarginLayoutParamsCompat.setMarginEnd(this.mPreservedParams, MarginLayoutParamsCompat.getMarginEnd(params));
if (this.leftMarginPercent != null) {
params.leftMargin = (int) (((float) (this.leftMarginPercent.isBaseWidth ? widthHint : heightHint)) * this.leftMarginPercent.percent);
}
if (this.topMarginPercent != null) {
if (this.topMarginPercent.isBaseWidth) {
base = widthHint;
} else {
base = heightHint;
}
params.topMargin = (int) (((float) base) * this.topMarginPercent.percent);
}
if (this.rightMarginPercent != null) {
if (this.rightMarginPercent.isBaseWidth) {
base = widthHint;
} else {
base = heightHint;
}
params.rightMargin = (int) (((float) base) * this.rightMarginPercent.percent);
}
if (this.bottomMarginPercent != null) {
if (this.bottomMarginPercent.isBaseWidth) {
base = widthHint;
} else {
base = heightHint;
}
params.bottomMargin = (int) (((float) base) * this.bottomMarginPercent.percent);
}
if (this.startMarginPercent != null) {
if (this.startMarginPercent.isBaseWidth) {
base = widthHint;
} else {
base = heightHint;
}
MarginLayoutParamsCompat.setMarginStart(params, (int) (((float) base) * this.startMarginPercent.percent));
}
if (this.endMarginPercent != null) {
if (this.endMarginPercent.isBaseWidth) {
base = widthHint;
} else {
base = heightHint;
}
MarginLayoutParamsCompat.setMarginEnd(params, (int) (((float) base) * this.endMarginPercent.percent));
}
if (Log.isLoggable(PercentLayoutHelper.TAG, 3)) {
Log.d(PercentLayoutHelper.TAG, "after fillMarginLayoutParams: (" + params.width + ", " + params.height + ")");
}
}
/**
* Place a single View when the layout direction is vertical
* ({@link FlexContainer#getFlexDirection()} is either {@link FlexDirection#COLUMN} or
* {@link FlexDirection#COLUMN_REVERSE}).
*
* @param view the View to be placed
* @param flexLine the {@link FlexLine} where the View belongs to
* @param isRtl {@code true} if the layout direction is right to left, {@code false}
* otherwise
* @param left the left position of the flex line where the View belongs to. The actual
* View's left position is shifted depending on the isLayoutRtl and alignItems
* attributes
* @param top the top position of the View, which the View's margin is already taken
* into account
* @param right the right position of the flex line where the View belongs to. The actual
* View's right position is shifted depending on the isLayoutRtl and alignItems
* attributes
* @param bottom the bottom position of the View, which the View's margin is already taken
* into account
* @see FlexContainer#getAlignItems()
* @see FlexContainer#setAlignItems(int)
* @see FlexItem#getAlignSelf()
*/
void layoutSingleChildVertical(View view, FlexLine flexLine, boolean isRtl,
int left, int top, int right, int bottom) {
FlexItem flexItem = (FlexItem) view.getLayoutParams();
int alignItems = mFlexContainer.getAlignItems();
if (flexItem.getAlignSelf() != AlignSelf.AUTO) {
// Expecting the values for alignItems and mAlignSelf match except for ALIGN_SELF_AUTO.
// Assigning the mAlignSelf value as alignItems should work.
alignItems = flexItem.getAlignSelf();
}
int crossSize = flexLine.mCrossSize;
switch (alignItems) {
case AlignItems.FLEX_START: // Intentional fall through
case AlignItems.STRETCH: // Intentional fall through
case AlignItems.BASELINE:
if (!isRtl) {
view.layout(left + flexItem.getMarginLeft(), top,
right + flexItem.getMarginLeft(), bottom);
} else {
view.layout(left - flexItem.getMarginRight(), top,
right - flexItem.getMarginRight(), bottom);
}
break;
case AlignItems.FLEX_END:
if (!isRtl) {
view.layout(
left + crossSize - view.getMeasuredWidth() - flexItem.getMarginRight(),
top,
right + crossSize - view.getMeasuredWidth() - flexItem.getMarginRight(),
bottom);
} else {
// If the flexWrap == WRAP_REVERSE, the direction of the
// flexEnd is flipped (from left to right).
view.layout(
left - crossSize + view.getMeasuredWidth() + flexItem.getMarginLeft(),
top,
right - crossSize + view.getMeasuredWidth() + flexItem.getMarginLeft(),
bottom);
}
break;
case AlignItems.CENTER:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)
view.getLayoutParams();
int leftFromCrossAxis = (crossSize - view.getMeasuredWidth()
+ MarginLayoutParamsCompat.getMarginStart(lp)
- MarginLayoutParamsCompat.getMarginEnd(lp)) / 2;
if (!isRtl) {
view.layout(left + leftFromCrossAxis, top, right + leftFromCrossAxis, bottom);
} else {
view.layout(left - leftFromCrossAxis, top, right - leftFromCrossAxis, bottom);
}
break;
}
}
/**
* Fills the margin fields of the passed {@link ViewGroup.MarginLayoutParams} object based
* on currently set percentage values and the current layout direction of the passed
* {@link View}.
*/
public void fillMarginLayoutParams(View view, ViewGroup.MarginLayoutParams params,
int widthHint, int heightHint) {
fillLayoutParams(params, widthHint, heightHint);
// Preserve the original margins, so we can restore them after the measure step.
mPreservedParams.leftMargin = params.leftMargin;
mPreservedParams.topMargin = params.topMargin;
mPreservedParams.rightMargin = params.rightMargin;
mPreservedParams.bottomMargin = params.bottomMargin;
MarginLayoutParamsCompat.setMarginStart(mPreservedParams,
MarginLayoutParamsCompat.getMarginStart(params));
MarginLayoutParamsCompat.setMarginEnd(mPreservedParams,
MarginLayoutParamsCompat.getMarginEnd(params));
int screenWidth = ScreenUtil.getScreenWidth(view.getContext());
if (leftMarginPercentX >= 0) {
params.leftMargin = Math.round(widthHint * leftMarginPercentX);
}
else if (leftMarginPercentY >= 0) {
params.leftMargin = Math.round(heightHint * leftMarginPercentY);
}
else if (leftMarginPercentScreenX >= 0) {
params.leftMargin = Math.round(screenWidth * leftMarginPercentScreenX);
}
if (topMarginPercentX >= 0) {
params.topMargin = Math.round(widthHint * topMarginPercentX);
}
else if (topMarginPercentY >= 0) {
params.topMargin = Math.round(heightHint * topMarginPercentY);
}
else if (topMarginPercentScreenX >= 0) {
params.topMargin = Math.round(screenWidth * topMarginPercentScreenX);
}
if (rightMarginPercentX >= 0) {
params.rightMargin = Math.round(widthHint * rightMarginPercentX);
}
else if (rightMarginPercentY >= 0) {
params.rightMargin = Math.round(heightHint * rightMarginPercentY);
}
else if (rightMarginPercentScreenX >= 0) {
params.rightMargin = Math.round(screenWidth * rightMarginPercentScreenX);
}
if (bottomMarginPercentX >= 0) {
params.bottomMargin = Math.round(widthHint * bottomMarginPercentX);
}
else if(bottomMarginPercentY >= 0){
params.bottomMargin = Math.round(heightHint * bottomMarginPercentY);
}
else if(bottomMarginPercentScreenX >= 0){
params.bottomMargin = Math.round(screenWidth * bottomMarginPercentScreenX);
}
boolean shouldResolveLayoutDirection = false;
if (startMarginPercent >= 0) {
MarginLayoutParamsCompat.setMarginStart(params,
Math.round(widthHint * startMarginPercent));
shouldResolveLayoutDirection = true;
}
if (endMarginPercent >= 0) {
MarginLayoutParamsCompat.setMarginEnd(params,
Math.round(widthHint * endMarginPercent));
shouldResolveLayoutDirection = true;
}
if (shouldResolveLayoutDirection && (view != null)) {
// Force the resolve pass so that start / end margins are propagated to the
// matching left / right fields
MarginLayoutParamsCompat.resolveLayoutDirection(params,
ViewCompat.getLayoutDirection(view));
}
if (DEBUG) {
Log.d(TAG, "after fillMarginLayoutParams: (" + params.width + ", " + params.height
+ ")");
}
}
/**
* Place a single View when the layout direction is vertical ({@link #mFlexDirection} is
* either {@link #FLEX_DIRECTION_COLUMN} or {@link #FLEX_DIRECTION_COLUMN_REVERSE}).
*
* @param view the View to be placed
* @param flexLine the {@link FlexLine} where the View belongs to
* @param isRtl {@code true} if the layout direction is right to left, {@code false}
* otherwise
* @param alignItems the align items attribute of this FlexboxLayout
* @param left the left position of the flex line where the View belongs to. The actual
* View's left position is shifted depending on the isRtl and alignItems
* attributes
* @param top the top position of the View, which the View's margin is already taken
* into account
* @param right the right position of the flex line where the View belongs to. The actual
* View's right position is shifted depending on the isRtl and alignItems
* attributes
* @param bottom the bottom position of the View, which the View's margin is already taken
* into account
* @see #getAlignItems()
* @see #setAlignItems(int)
* @see LayoutParams#alignSelf
*/
private void layoutSingleChildVertical(View view, FlexLine flexLine, boolean isRtl,
int alignItems, int left, int top, int right, int bottom) {
LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (lp.alignSelf != LayoutParams.ALIGN_SELF_AUTO) {
// Expecting the values for alignItems and alignSelf match except for ALIGN_SELF_AUTO.
// Assigning the alignSelf value as alignItems should work.
alignItems = lp.alignSelf;
}
int crossSize = flexLine.mCrossSize;
switch (alignItems) {
case ALIGN_ITEMS_FLEX_START: // Intentional fall through
case ALIGN_ITEMS_STRETCH: // Intentional fall through
case ALIGN_ITEMS_BASELINE:
if (!isRtl) {
view.layout(left + lp.leftMargin, top, right + lp.leftMargin, bottom);
} else {
view.layout(left - lp.rightMargin, top, right - lp.rightMargin, bottom);
}
break;
case ALIGN_ITEMS_FLEX_END:
if (!isRtl) {
view.layout(left + crossSize - view.getMeasuredWidth() - lp.rightMargin,
top, right + crossSize - view.getMeasuredWidth() - lp.rightMargin,
bottom);
} else {
// If the flexWrap == FLEX_WRAP_WRAP_REVERSE, the direction of the
// flexEnd is flipped (from left to right).
view.layout(left - crossSize + view.getMeasuredWidth() + lp.leftMargin, top,
right - crossSize + view.getMeasuredWidth() + lp.leftMargin,
bottom);
}
break;
case ALIGN_ITEMS_CENTER:
int leftFromCrossAxis = (crossSize - view.getMeasuredWidth()
+ MarginLayoutParamsCompat.getMarginStart(lp)
- MarginLayoutParamsCompat.getMarginEnd(lp)) / 2;
if (!isRtl) {
view.layout(left + leftFromCrossAxis, top, right + leftFromCrossAxis, bottom);
} else {
view.layout(left - leftFromCrossAxis, top, right - leftFromCrossAxis, bottom);
}
break;
}
}
private void applyXmlAttributes(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
final TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.FloatingSearchView, defStyleAttr, defStyleRes);
// Search bar width
View suggestionsContainer = findViewById(R.id.fsv_suggestions_container);
int searchBarWidth = a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_searchBarWidth,
mSearchContainer.getLayoutParams().width);
mSearchContainer.getLayoutParams().width = searchBarWidth;
suggestionsContainer.getLayoutParams().width = searchBarWidth;
// Divider
mDivider.setBackgroundDrawable(a.getDrawable(R.styleable.FloatingSearchView_android_divider));
int dividerHeight = a.getDimensionPixelSize(R.styleable.FloatingSearchView_android_dividerHeight, -1);
MarginLayoutParams dividerLP = (MarginLayoutParams) mDivider.getLayoutParams();
if(mDivider.getBackground() != null && dividerHeight != -1)
dividerLP.height = dividerHeight;
float maxShadowSize = mSearchBackground.getMaxShadowSize();
float cornerRadius = mSearchBackground.getCornerRadius();
int horizontalPadding = (int) (RoundRectDrawableWithShadow.calculateHorizontalPadding(
maxShadowSize, cornerRadius, false) + .5f);
dividerLP.setMargins(horizontalPadding, dividerLP.topMargin, horizontalPadding, dividerLP.bottomMargin);
mDivider.setLayoutParams(dividerLP);
// Content inset
MarginLayoutParams searchParams = (MarginLayoutParams) mSearchInput.getLayoutParams();
int contentInsetStart = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetStart,
MarginLayoutParamsCompat.getMarginStart(searchParams));
int contentInsetEnd = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetEnd,
MarginLayoutParamsCompat.getMarginEnd(searchParams));
MarginLayoutParamsCompat.setMarginStart(searchParams, contentInsetStart);
MarginLayoutParamsCompat.setMarginEnd(searchParams, contentInsetEnd);
// anything else
setLogo(a.getDrawable(R.styleable.FloatingSearchView_logo));
setContentBackgroundColor(a.getColor(R.styleable.FloatingSearchView_fsv_contentBackgroundColor, DEFAULT_CONTENT_COLOR));
setRadius(a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_cornerRadius, ViewUtils.dpToPx(DEFAULT_RADIUS)));
inflateMenu(a.getResourceId(R.styleable.FloatingSearchView_fsv_menu, 0));
setPopupTheme(a.getResourceId(R.styleable.FloatingSearchView_popupTheme, 0));
setHint(a.getString(R.styleable.FloatingSearchView_android_hint));
setIcon(a.getDrawable(R.styleable.FloatingSearchView_fsv_icon));
a.recycle();
}
private void applyXmlAttributes(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
final TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.FloatingSearchView, defStyleAttr, defStyleRes);
// Search bar width
View suggestionsContainer = findViewById(R.id.fsv_suggestions_container);
int searchBarWidth = a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_searchBarWidth,
mSearchContainer.getLayoutParams().width);
mSearchContainer.getLayoutParams().width = searchBarWidth;
suggestionsContainer.getLayoutParams().width = searchBarWidth;
// Divider
mDivider.setBackgroundDrawable(a.getDrawable(R.styleable.FloatingSearchView_android_divider));
int dividerHeight = a.getDimensionPixelSize(R.styleable.FloatingSearchView_android_dividerHeight, -1);
MarginLayoutParams dividerLP = (MarginLayoutParams) mDivider.getLayoutParams();
if(mDivider.getBackground() != null && dividerHeight != -1)
dividerLP.height = dividerHeight;
float maxShadowSize = mSearchBackground.getMaxShadowSize();
float cornerRadius = mSearchBackground.getCornerRadius();
int horizontalPadding = (int) (RoundRectDrawableWithShadow.calculateHorizontalPadding(
maxShadowSize, cornerRadius, false) + .5f);
dividerLP.setMargins(horizontalPadding, dividerLP.topMargin, horizontalPadding, dividerLP.bottomMargin);
mDivider.setLayoutParams(dividerLP);
// Content inset
MarginLayoutParams searchParams = (MarginLayoutParams) mSearchInput.getLayoutParams();
int contentInsetStart = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetStart,
MarginLayoutParamsCompat.getMarginStart(searchParams));
int contentInsetEnd = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetEnd,
MarginLayoutParamsCompat.getMarginEnd(searchParams));
MarginLayoutParamsCompat.setMarginStart(searchParams, contentInsetStart);
MarginLayoutParamsCompat.setMarginEnd(searchParams, contentInsetEnd);
// anything else
setLogo(a.getDrawable(R.styleable.FloatingSearchView_logo));
setContentBackgroundColor(a.getColor(R.styleable.FloatingSearchView_fsv_contentBackgroundColor, DEFAULT_CONTENT_COLOR));
setRadius(a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_cornerRadius, ViewUtils.dpToPx(DEFAULT_RADIUS)));
inflateMenu(a.getResourceId(R.styleable.FloatingSearchView_fsv_menu, 0));
setPopupTheme(a.getResourceId(R.styleable.FloatingSearchView_popupTheme, 0));
setHint(a.getString(R.styleable.FloatingSearchView_android_hint));
setIcon(a.getDrawable(R.styleable.FloatingSearchView_fsv_icon));
a.recycle();
}
private void addRowForAnswer(final Context context, final TableLayout tableLayout, final DecisionTree.Question question, final DecisionTree.BaseButton answer) {
final TableRow row = new TableRow(context);
final TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, UiUtils.getPxForDpResource(context, R.dimen.standard_large_margin), 0, 0);
tableLayout.addView(row, params);
final LinearLayout layoutVertical = new LinearLayout(context);
layoutVertical.setOrientation(LinearLayout.VERTICAL);
final TextView textViewAnswer = new AppCompatTextView(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textViewAnswer.setTextAppearance(R.style.TextAppearance_AppCompat_Subhead);
} else {
//noinspection deprecation
textViewAnswer.setTextAppearance(context, R.style.TextAppearance_AppCompat_Subhead);
}
textViewAnswer.setText(answer.getText());
layoutVertical.addView(textViewAnswer,
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
final LinearLayout layoutHorizontal = new LinearLayout(context);
layoutHorizontal.setOrientation(LinearLayout.HORIZONTAL);
final LinearLayout.LayoutParams paramsHorizontal = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsHorizontal.setMargins(0, UiUtils.getPxForDpResource(context, R.dimen.standard_margin), 0, 0);
layoutVertical.addView(layoutHorizontal, paramsHorizontal);
final BitmapDrawable icon = getIcon(context, answer);
final ImageView imageIcon = new ImageView(context);
imageIcon.setImageDrawable(icon);
layoutHorizontal.addView(imageIcon);
final LinearLayout.LayoutParams paramsImage = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// TODO: Use a custom FlowTable class to avoid items going off the right edge of the screen
// when there are too many.
final Singleton singleton = getSingleton();
for (int i = 0; i < answer.getExamplesCount(); i++) {
final String iconName = answer.getExampleIconName(question.getId(), i);
final BitmapDrawable iconExample = singleton.getIconDrawable(context, iconName);
final ImageButton imageExample = new ImageButton(context);
//Remove the space between the image and the outside of the button:
imageExample.setPadding(0, 0, 0, 0);
imageExample.setImageDrawable(iconExample);
//Needed to make the image expand as a transition into the SubjectViewerActivity,
//which uses the same name in fragment_subject.xml
ViewCompat.setTransitionName(imageExample, getString(R.string.transition_subject_image));
//This requires API level 17: paramsImage.setMarginStart(getPxForDp(activity, MARGIN_MEDIUM_DP));
//imageExample.setLayoutParams(paramsImage);
MarginLayoutParamsCompat.setMarginStart(paramsImage, UiUtils.getPxForDpResource(context, R.dimen.standard_large_margin));
final int answerIndex = i;
imageExample.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
// Perform action on click
onExampleImageClicked(v, answer, answerIndex);
}
});
layoutHorizontal.addView(imageExample, paramsImage);
}
row.addView(layoutVertical,
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
}