下面列出了android.support.v4.view.ViewCompat#setLayoutDirection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initView(Context context) {
ViewCompat.setLayoutDirection(this, ViewCompat.LAYOUT_DIRECTION_LTR);
if (mIndicatorType == 0) {
for (int i = 0; i < mPinLength; i++) {
View dot = new View(context);
emptyDot(dot);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mDotDiameter,
mDotDiameter);
params.setMargins(mDotSpacing, 0, mDotSpacing, 0);
dot.setLayoutParams(params);
addView(dot);
}
} else if (mIndicatorType == 2) {
setLayoutTransition(new LayoutTransition());
}
}
private void initScrollView() {
mScrollView = new InternalScrollView(getContext());
mScrollView.setPadding(0, 0, 0, mContentPadding - mActionPadding_h);
mScrollView.setClipToPadding(false);
mScrollView.setFillViewport(true);
mScrollView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
ViewCompat.setLayoutDirection(mScrollView, View.LAYOUT_DIRECTION_INHERIT);
}
private void initListView() {
mListView = new InternalListView(getContext());
mListView.setDividerHeight(0);
mListView.setCacheColorHint(0x00000000);
mListView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mListView.setClipToPadding(false);
mListView.setSelector(BlankDrawable.getInstance());
mListView.setPadding(0, 0, 0, mContentPadding - mActionPadding_h);
mListView.setVerticalFadingEdgeEnabled(false);
mListView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
ViewCompat.setLayoutDirection(mListView, ViewCompat.LAYOUT_DIRECTION_INHERIT);
mAdapter = new InternalAdapter();
mListView.setAdapter(mAdapter);
}
public void correctDiretion()
{
boolean numIsRtl=true;
if (ViewCompat.getLayoutDirection(this) != View.LAYOUT_DIRECTION_RTL) {
numIsRtl = false;
}
ViewCompat.setLayoutDirection(this, View.LAYOUT_DIRECTION_LTR);
this.setGravity(numIsRtl ? Gravity.LEFT : Gravity.RIGHT);
}
private void initScrollView() {
mScrollView = new InternalScrollView(getContext());
mScrollView.setPadding(0, 0, 0, mContentPadding - mActionPadding);
mScrollView.setClipToPadding(false);
mScrollView.setFillViewport(true);
mScrollView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
ViewCompat.setLayoutDirection(mScrollView, ViewCompat.LAYOUT_DIRECTION_INHERIT);
}
private void initListView() {
mListView = new InternalListView(getContext());
mListView.setDividerHeight(0);
mListView.setCacheColorHint(0x00000000);
mListView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mListView.setClipToPadding(false);
mListView.setSelector(BlankDrawable.getInstance());
mListView.setPadding(0, 0, 0, mContentPadding - mActionPadding);
mListView.setVerticalFadingEdgeEnabled(false);
mListView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
ViewCompat.setLayoutDirection(mListView, ViewCompat.LAYOUT_DIRECTION_INHERIT);
mAdapter = new InternalAdapter();
mListView.setAdapter(mAdapter);
}
@UiThread
@Override
protected ChipsLayoutManager getLayoutManager() {
ChipsLayoutManager layoutManager = super.getLayoutManager();
if (activityTestRule.getActivity() != null) {
View recyclerView = activityTestRule.getActivity().findViewById(R.id.rvTest);
ViewCompat.setLayoutDirection(recyclerView, ViewCompat.LAYOUT_DIRECTION_RTL);
}
return layoutManager;
}
@Test
@Config(sdk = 16)
public void givenLessThanSdk17TouchWithinToggle_whenOnTouchEvent_thenPasswordVisibilityToggled() {
ViewCompat.setLayoutDirection(passwordView, ViewCompat.LAYOUT_DIRECTION_RTL);
passwordView.setPasswordVisible(false);
float xPosition = passwordView.getRight() - 10;
float yPosition = passwordView.getTop() + 10;
fireActionUpTouchEvent(passwordView, xPosition, yPosition);
assertThat(passwordView)
.hasVisiblePassword();
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
// For correct display in RTL locales, we need to set the layout direction of the
// fragment's top view.
ViewCompat.setLayoutDirection(view, ViewCompat.LAYOUT_DIRECTION_LOCALE);
return view;
}
public void setLayoutDirection(final boolean isRtlLanguage) {
final int layoutDirection = isRtlLanguage ? ViewCompat.LAYOUT_DIRECTION_RTL
: ViewCompat.LAYOUT_DIRECTION_LTR;
ViewCompat.setLayoutDirection(mSuggestionStripView, layoutDirection);
ViewCompat.setLayoutDirection(mSuggestionsStrip, layoutDirection);
ViewCompat.setLayoutDirection(mImportantNoticeStrip, layoutDirection);
}
/**
* Sets the contents and state of the view for the given suggestion.
*
* @param suggestionItem The omnibox suggestion item this view represents.
* @param suggestionDelegate The suggestion delegate.
* @param position Position of the suggestion in the dropdown list.
* @param useDarkColors Whether dark colors should be used for fonts and icons.
*/
public void init(OmniboxResultItem suggestionItem,
OmniboxSuggestionDelegate suggestionDelegate,
int position, boolean useDarkColors) {
ViewCompat.setLayoutDirection(this, ViewCompat.getLayoutDirection(mUrlBar));
// Update the position unconditionally.
mPosition = position;
jumpDrawablesToCurrentState();
boolean colorsChanged = mUseDarkColors == null || mUseDarkColors != useDarkColors;
if (suggestionItem.equals(mSuggestionItem) && !colorsChanged) return;
mUseDarkColors = useDarkColors;
if (colorsChanged) {
mContentsView.mTextLine1.setTextColor(getStandardFontColor());
setRefineIcon(true);
}
mSuggestionItem = suggestionItem;
mSuggestion = suggestionItem.getSuggestion();
mSuggestionDelegate = suggestionDelegate;
// Reset old computations.
mContentsView.resetTextWidths();
mContentsView.mAnswerImage.setVisibility(GONE);
mContentsView.mAnswerImage.getLayoutParams().height = 0;
mContentsView.mAnswerImage.getLayoutParams().width = 0;
mContentsView.mAnswerImage.setImageDrawable(null);
mContentsView.mAnswerImageMaxSize = 0;
mContentsView.mTextLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_first_line_text_size));
mContentsView.mTextLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_second_line_text_size));
// Suggestions with attached answers are rendered with rich results regardless of which
// suggestion type they are.
if (mSuggestion.hasAnswer()) {
setAnswer(mSuggestion.getAnswer());
mContentsView.setSuggestionIcon(SUGGESTION_ICON_MAGNIFIER, colorsChanged);
mContentsView.mTextLine2.setVisibility(VISIBLE);
setRefinable(true);
return;
} else {
mNumAnswerLines = 1;
mContentsView.mTextLine2.setEllipsize(null);
mContentsView.mTextLine2.setSingleLine();
}
boolean sameAsTyped =
suggestionItem.getMatchedQuery().equalsIgnoreCase(mSuggestion.getDisplayText());
int suggestionType = mSuggestion.getType();
if (mSuggestion.isUrlSuggestion()) {
if (mSuggestion.isStarred()) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_BOOKMARK, colorsChanged);
} else if (suggestionType == OmniboxSuggestionType.HISTORY_URL) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_HISTORY, colorsChanged);
} else {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_GLOBE, colorsChanged);
}
boolean urlShown = !TextUtils.isEmpty(mSuggestion.getUrl());
boolean urlHighlighted = false;
if (urlShown) {
urlHighlighted = setUrlText(suggestionItem);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
setSuggestedQuery(suggestionItem, true, urlShown, urlHighlighted);
setRefinable(!sameAsTyped);
} else {
@SuggestionIcon int suggestionIcon = SUGGESTION_ICON_MAGNIFIER;
if (suggestionType == OmniboxSuggestionType.VOICE_SUGGEST) {
suggestionIcon = SUGGESTION_ICON_VOICE;
} else if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PERSONALIZED)
|| (suggestionType == OmniboxSuggestionType.SEARCH_HISTORY)) {
// Show history icon for suggestions based on user queries.
suggestionIcon = SUGGESTION_ICON_HISTORY;
}
mContentsView.setSuggestionIcon(suggestionIcon, colorsChanged);
setRefinable(!sameAsTyped);
setSuggestedQuery(suggestionItem, false, false, false);
if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PROFILE)) {
showDescriptionLine(SpannableString.valueOf(mSuggestion.getDescription()), false);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
}
}
/**
* Sets the contents and state of the view for the given suggestion.
*
* @param suggestionItem The omnibox suggestion item this view represents.
* @param suggestionDelegate The suggestion delegate.
* @param position Position of the suggestion in the dropdown list.
* @param useDarkColors Whether dark colors should be used for fonts and icons.
*/
public void init(OmniboxResultItem suggestionItem,
OmniboxSuggestionDelegate suggestionDelegate,
int position, boolean useDarkColors) {
ViewCompat.setLayoutDirection(this, ViewCompat.getLayoutDirection(mUrlBar));
// Update the position unconditionally.
mPosition = position;
jumpDrawablesToCurrentState();
boolean colorsChanged = mUseDarkColors == null || mUseDarkColors != useDarkColors;
if (suggestionItem.equals(mSuggestionItem) && !colorsChanged) return;
mUseDarkColors = useDarkColors;
if (colorsChanged) {
mContentsView.mTextLine1.setTextColor(getStandardFontColor());
setRefineIcon(true);
}
mSuggestionItem = suggestionItem;
mSuggestion = suggestionItem.getSuggestion();
mSuggestionDelegate = suggestionDelegate;
// Reset old computations.
mContentsView.resetTextWidths();
mContentsView.mAnswerImage.setVisibility(GONE);
mContentsView.mAnswerImage.getLayoutParams().height = 0;
mContentsView.mAnswerImage.getLayoutParams().width = 0;
mContentsView.mAnswerImage.setImageDrawable(null);
mContentsView.mAnswerImageMaxSize = 0;
mContentsView.mTextLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_first_line_text_size));
mContentsView.mTextLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_second_line_text_size));
// Suggestions with attached answers are rendered with rich results regardless of which
// suggestion type they are.
if (mSuggestion.hasAnswer()) {
setAnswer(mSuggestion.getAnswer());
mContentsView.setSuggestionIcon(SUGGESTION_ICON_MAGNIFIER, colorsChanged);
mContentsView.mTextLine2.setVisibility(VISIBLE);
setRefinable(true);
return;
} else {
mNumAnswerLines = 1;
mContentsView.mTextLine2.setEllipsize(null);
mContentsView.mTextLine2.setSingleLine();
}
boolean sameAsTyped =
suggestionItem.getMatchedQuery().equalsIgnoreCase(mSuggestion.getDisplayText());
int suggestionType = mSuggestion.getType();
if (mSuggestion.isUrlSuggestion()) {
if (mSuggestion.isStarred()) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_BOOKMARK, colorsChanged);
} else if (suggestionType == OmniboxSuggestionType.HISTORY_URL) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_HISTORY, colorsChanged);
} else {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_GLOBE, colorsChanged);
}
boolean urlShown = !TextUtils.isEmpty(mSuggestion.getUrl());
boolean urlHighlighted = false;
if (urlShown) {
urlHighlighted = setUrlText(suggestionItem);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
setSuggestedQuery(suggestionItem, true, urlShown, urlHighlighted);
setRefinable(!sameAsTyped);
} else {
@SuggestionIcon int suggestionIcon = SUGGESTION_ICON_MAGNIFIER;
if (suggestionType == OmniboxSuggestionType.VOICE_SUGGEST) {
suggestionIcon = SUGGESTION_ICON_VOICE;
} else if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PERSONALIZED)
|| (suggestionType == OmniboxSuggestionType.SEARCH_HISTORY)) {
// Show history icon for suggestions based on user queries.
suggestionIcon = SUGGESTION_ICON_HISTORY;
}
mContentsView.setSuggestionIcon(suggestionIcon, colorsChanged);
setRefinable(!sameAsTyped);
setSuggestedQuery(suggestionItem, false, false, false);
if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PROFILE)) {
showDescriptionLine(SpannableString.valueOf(mSuggestion.getDescription()), false);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
}
}
/**
* Sets the contents and state of the view for the given suggestion.
*
* @param suggestionItem The omnibox suggestion item this view represents.
* @param suggestionDelegate The suggestion delegate.
* @param position Position of the suggestion in the dropdown list.
* @param useDarkColors Whether dark colors should be used for fonts and icons.
*/
public void init(OmniboxResultItem suggestionItem,
OmniboxSuggestionDelegate suggestionDelegate,
int position, boolean useDarkColors) {
ViewCompat.setLayoutDirection(this, ViewCompat.getLayoutDirection(mUrlBar));
// Update the position unconditionally.
mPosition = position;
jumpDrawablesToCurrentState();
boolean colorsChanged = mUseDarkColors == null || mUseDarkColors != useDarkColors;
if (suggestionItem.equals(mSuggestionItem) && !colorsChanged) return;
mUseDarkColors = useDarkColors;
if (colorsChanged) {
mContentsView.mTextLine1.setTextColor(getStandardFontColor());
setRefineIcon(true);
}
mSuggestionItem = suggestionItem;
mSuggestion = suggestionItem.getSuggestion();
mSuggestionDelegate = suggestionDelegate;
// Reset old computations.
mContentsView.resetTextWidths();
mContentsView.mAnswerImage.setVisibility(GONE);
mContentsView.mAnswerImage.getLayoutParams().height = 0;
mContentsView.mAnswerImage.getLayoutParams().width = 0;
mContentsView.mAnswerImage.setImageDrawable(null);
mContentsView.mAnswerImageMaxSize = 0;
mContentsView.mTextLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_first_line_text_size));
mContentsView.mTextLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimension(R.dimen.omnibox_suggestion_second_line_text_size));
// Suggestions with attached answers are rendered with rich results regardless of which
// suggestion type they are.
if (mSuggestion.hasAnswer()) {
setAnswer(mSuggestion.getAnswer());
mContentsView.setSuggestionIcon(SUGGESTION_ICON_MAGNIFIER, colorsChanged);
mContentsView.mTextLine2.setVisibility(VISIBLE);
setRefinable(true);
return;
} else {
mNumAnswerLines = 1;
mContentsView.mTextLine2.setEllipsize(null);
mContentsView.mTextLine2.setSingleLine();
}
boolean sameAsTyped =
suggestionItem.getMatchedQuery().equalsIgnoreCase(mSuggestion.getDisplayText());
int suggestionType = mSuggestion.getType();
if (mSuggestion.isUrlSuggestion()) {
if (mSuggestion.isStarred()) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_BOOKMARK, colorsChanged);
} else if (suggestionType == OmniboxSuggestionType.HISTORY_URL) {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_HISTORY, colorsChanged);
} else {
mContentsView.setSuggestionIcon(SUGGESTION_ICON_GLOBE, colorsChanged);
}
boolean urlShown = !TextUtils.isEmpty(mSuggestion.getUrl());
boolean urlHighlighted = false;
if (urlShown) {
urlHighlighted = setUrlText(suggestionItem);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
setSuggestedQuery(suggestionItem, true, urlShown, urlHighlighted);
setRefinable(!sameAsTyped
&& suggestionType != OmniboxSuggestionType.PHYSICAL_WEB_OVERFLOW);
} else {
@SuggestionIcon int suggestionIcon = SUGGESTION_ICON_MAGNIFIER;
if (suggestionType == OmniboxSuggestionType.VOICE_SUGGEST) {
suggestionIcon = SUGGESTION_ICON_VOICE;
} else if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PERSONALIZED)
|| (suggestionType == OmniboxSuggestionType.SEARCH_HISTORY)) {
// Show history icon for suggestions based on user queries.
suggestionIcon = SUGGESTION_ICON_HISTORY;
}
mContentsView.setSuggestionIcon(suggestionIcon, colorsChanged);
setRefinable(!sameAsTyped);
setSuggestedQuery(suggestionItem, false, false, false);
if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PROFILE)) {
showDescriptionLine(SpannableString.valueOf(mSuggestion.getDescription()), false);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
}
}
/**
* Set the layout direction of this Dialog
*
* @param direction The layout direction value. Can be {@link View#LAYOUT_DIRECTION_LTR}, {@link View#LAYOUT_DIRECTION_RTL} or {@link View#LAYOUT_DIRECTION_LOCALE}
*
* @return The Dialog for chaining methods.
*/
public Dialog layoutDirection(int direction) {
ViewCompat.setLayoutDirection(mCardView, direction);
return this;
}
public static void setLayoutDirection(View view, int direction) {
ViewCompat.setLayoutDirection(view, direction);
}
/**
* Set the layout direction of this Dialog
*
* @param direction The layout direction value. Can be {@link View#LAYOUT_DIRECTION_LTR}, {@link View#LAYOUT_DIRECTION_RTL} or {@link View#LAYOUT_DIRECTION_LOCALE}
* @return The Dialog for chaining methods.
*/
public Dialog layoutDirection(int direction) {
ViewCompat.setLayoutDirection(mCardView, direction);
return this;
}