下面列出了android.view.View#setId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onChildViewAdded(View parent, View child) {
if (parent == ToolButtonGroup.this && child instanceof ToolButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = generateViewId();
child.setId(id);
}
((ToolButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
public void addFixedView(View view, boolean isRight) {
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
LayoutParams hsvParams = (LayoutParams) hsv_toolbar.getLayoutParams();
if (view.getId() <= 0) {
view.setId(getIdValue());
}
if (isRight) {
params.addRule(ALIGN_PARENT_RIGHT);
hsvParams.addRule(LEFT_OF, view.getId());
} else {
params.addRule(ALIGN_PARENT_LEFT);
hsvParams.addRule(RIGHT_OF, view.getId());
}
addView(view, params);
hsv_toolbar.setLayoutParams(hsvParams);
}
public void onChildViewAdded(View parent, View child) {
if (parent == ToggleButtonGroup.this && child instanceof Checkable) {
if (child.getId() == View.NO_ID) {
child.setId(generateIdForView(child));
}
if (child instanceof ToggleButton) {
setStateTracker((ToggleButton) child);
} else if (child instanceof CompoundButton) {
setStateTracker((CompoundButton) child);
}
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
private void createVideoView(final RemoteParticipant remoteParticipant) {
Handler mainHandler = new Handler(videoConferenceActivity.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
View rowView = videoConferenceActivity.getLayoutInflater().inflate(R.layout.peer_video, null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 20);
rowView.setLayoutParams(lp);
int rowId = View.generateViewId();
rowView.setId(rowId);
views_container.addView(rowView);
SurfaceViewRenderer videoView = (SurfaceViewRenderer)((ViewGroup)rowView).getChildAt(0);
remoteParticipant.setVideoView(videoView);
videoView.setMirror(false);
EglBase rootEglBase = EglBase.create();
videoView.init(rootEglBase.getEglBaseContext(), null);
videoView.setZOrderMediaOverlay(true);
View textView = ((ViewGroup)rowView).getChildAt(1);
remoteParticipant.setParticipantNameText((TextView) textView);
remoteParticipant.setView(rowView);
}
};
mainHandler.post(myRunnable);
}
/**
* 计算子view的个数并添加到list中
*/
private void calculateChild() {
mViewList.clear();
mItemList.clear();
for (int i = 0; i < mZoomHoverAdapter.getCount(); i++) {
View childView = mZoomHoverAdapter.getView(this, i, mZoomHoverAdapter.getItem(i));
//添加view到mViewList
mViewList.add(childView);
childView.setId(i + 1);
if (mSpanMap.containsKey(i)) {
int tempRowSpan = mSpanMap.get(i).getRowSpan();
int tempColumnSpan = mSpanMap.get(i).getColumnSpan();
tempRowSpan = tempRowSpan >= 1 ? tempRowSpan : 1;
tempColumnSpan = tempColumnSpan >= 1 ? tempColumnSpan : 1;
if (tempColumnSpan > mColumnNum) {
tempColumnSpan = mColumnNum;
}
//添加item到mItemList
mItemList.add(new ZoomHoverItem(tempRowSpan, tempColumnSpan));
} else {
//添加item到mItemList
mItemList.add(new ZoomHoverItem(1, 1));
}
}
}
/**
* {@inheritDoc}
*/
public void onChildViewAdded(View parent, View child) {
if (parent == ToggleGroup.this && child instanceof CompoundButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
if (Build.VERSION.SDK_INT < 17)
id = child.hashCode();
else
id = View.generateViewId();
child.setId(id);
}
((CompoundButton) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
private void bindFooterView(BaseCell cell) {
if (cell != null && cell.isValid()) {
View footer = getViewFromRecycler(cell);
if (footer != null) {
footer.setId(R.id.TANGRAM_BANNER_FOOTER_ID);
//为了解决在 item 复用过程中,itemView 的 layoutParams 复用造成 layout 错误,这里要提供一个新的 layoutParams。
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.topMargin = cell.style.margin[Style.MARGIN_TOP_INDEX];
lp.leftMargin = cell.style.margin[Style.MARGIN_LEFT_INDEX];
lp.bottomMargin = cell.style.margin[Style.MARGIN_BOTTOM_INDEX];
lp.rightMargin = cell.style.margin[Style.MARGIN_RIGHT_INDEX];
addView(footer, lp);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(getCustomLayout(), container, false);
gridView = (GridView) view.findViewById(R.id.grid);
View emptyView = getEmptyView();
if (emptyView != null)
{
emptyView.setId(android.R.id.empty);
((ViewGroup) gridView.getParent()).addView(emptyView);
gridView.setEmptyView(emptyView);
}
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Log.d("KlyphFragment2", "onCreateView");
View view = inflater.inflate(getCustomLayout(), container, false);
gridView = (KlyphGridView) view.findViewById(R.id.grid);
View emptyView = getEmptyView();
if (emptyView != null)
{
emptyView.setId(android.R.id.empty);
((ViewGroup) gridView.getParent()).addView(emptyView);
gridView.setEmptyView(emptyView);
}
// Now set the ScrollView as the refreshable view, and the refresh listener (this)
if (requestNewestType != Query.NONE)
attachViewToPullToRefresh();
return view;
}
/**
* {@inheritDoc}
*/
@Override
public void onChildViewAdded(View parent, View child) {
if (parent == RadioGridGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = ViewUtils.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/** {@inheritDoc} */
@Override
public void onChildViewAdded(View parent, View child) {
if (parent == CheckGroup.this && child instanceof CheckBox) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((CheckBox) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
@Test
public void withIdTest() {
View view = new View(context);
view.setId(R.id.testId1);
assertTrue(withId(is(R.id.testId1)).matches(view));
assertFalse(withId(is(R.id.testId2)).matches(view));
assertFalse(withId(is(1234)).matches(view));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
parent.getContext();
String title = titleList.get(position);
if (convertView == null) {
convertView = newTabIndicator(parent.getContext(), title, false);
convertView.setId(ids.get(position)); // 设置ID.
} else {
// ... ...
}
return convertView;
}
/**
* 创建半透明矩形 View
*
* @param alpha 透明值
* @return 半透明 View
*/
private static View createTranslucentStatusBarView(Activity activity, int alpha) {
// 绘制一个和状态栏一样高的矩形
View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
return statusBarView;
}
/**
* 生成一个和状态栏大小相同的半透明矩形条
*
* @param activity 需要设置的activity
* @param color 状态栏颜色值
* @param alpha 透明值
* @return 状态栏矩形条
*/
private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
// 绘制一个和状态栏一样高的矩形
View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
return statusBarView;
}
/**
* initializes basic views for this {@link SettingsObject}
* such as the title, summary, and icon.
* you should override this method and initialize all other views your
* {@link SettingsObject} contains (e.g. for {@link CheckBoxSettingsObject},
* initialize the {@link android.widget.CheckBox})
* @param root the root view containing this entire {@link SettingsObject}
*/
public void initializeViews(View root)
{
int rootId = View.generateViewId();
root.setId(rootId);
individualSettingsRootId = rootId;
TextView tvTitle = root.findViewById(textViewTitleId);
tvTitle.setText(getTitle());
if (textViewSummaryId != null)
{
TextView tvSummary = root.findViewById(textViewSummaryId);
String summary;
if (useValueAsSummary())
{
summary = getValueHumanReadable();
}
else
{
summary = getSummary();
}
if (summary != null &&
summary.isEmpty() == false)
{
tvSummary.setText(summary);
}
else
{
tvSummary.setVisibility(View.GONE);
}
}
if(imageViewIconId != null)
{
ImageView ivIcon = root.findViewById(imageViewIconId);
if(iconDrawable != null)
{
ivIcon.setImageDrawable(iconDrawable);
}
//the user specifically set the value to null
//which means they want the settings object
//to align as if it has an icon
else if(iconDrawableId == null)
{
ivIcon.setImageDrawable(null);
}
//the user does NOT want the settings object to align
else if (iconDrawableId.equals(NO_ICON_DONT_ALIGN))
{
ivIcon.setVisibility(View.GONE);
}
//the user wants an actual icon
else
{
ivIcon.setImageResource(iconDrawableId);
}
}
}
private void setInputChild(View child) {
this.child = child;
if (child.isDuplicateParentStateEnabled())
container.setDuplicateParentStateEnabled(true);
if (child instanceof ViewGroup && ((ViewGroup) child).addStatesFromChildren())
container.setAddStatesFromChildren(true);
if (child.getId() == NO_ID)
child.setId(R.id.carbon_input);
if (child instanceof EditText) {
final EditText editText = (EditText) child;
if (labelTextView.getText().length() == 0)
setLabel(editText.getHint());
editText.addOnValidChangedListener(valid -> {
updateError(valid);
updateCounter(editText);
});
showPasswordImageView.setOnTouchListener((view, motionEvent) -> {
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
transformationMethod = editText.getTransformationMethod();
editText.setTransformationMethod(null);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
editText.setTransformationMethod(transformationMethod);
}
editText.setSelection(selectionStart, selectionEnd);
return false;
});
clearImageView.setOnClickListener(view -> editText.setText(""));
labelTextView.setInAnimator(null);
labelTextView.setOutAnimator(null);
setLabel(label);
errorTextView.setInAnimator(null);
errorTextView.setOutAnimator(null);
updateError(editText.isValid());
updateHint(editText);
updateCounter(editText);
labelTextView.setInAnimator(AnimUtils.getFlyInAnimator());
labelTextView.setOutAnimator(AnimUtils.getFadeOutAnimator());
errorTextView.setInAnimator(AnimUtils.getFadeInAnimator());
errorTextView.setOutAnimator(AnimUtils.getFadeOutAnimator());
} else if (child instanceof InputView) {
InputView inputView = (InputView) child;
inputView.addOnValidChangedListener(this::updateError);
labelTextView.setInAnimator(null);
labelTextView.setOutAnimator(null);
errorTextView.setInAnimator(null);
errorTextView.setOutAnimator(null);
updateError(inputView.isValid());
updateHint(child);
labelTextView.setInAnimator(AnimUtils.getFlyInAnimator());
labelTextView.setOutAnimator(AnimUtils.getFadeOutAnimator());
errorTextView.setInAnimator(AnimUtils.getFadeInAnimator());
errorTextView.setOutAnimator(AnimUtils.getFadeOutAnimator());
}
if (actionButton != ActionButton.None) {
int buttonPadding = getResources().getDimensionPixelSize(R.dimen.carbon_padding) + getResources().getDimensionPixelSize(R.dimen.carbon_iconSize);
child.setPadding(child.getPaddingLeft(), child.getPaddingTop(), child.getPaddingRight() + buttonPadding, child.getPaddingBottom());
}
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setId(mId);
}
public <T extends View> T replaceViewithTagToRemove(View view, String viewIdentifier, String tagToRemove) {
Context context = view.getContext();
View newView = null;
if (viewIdentifier.startsWith("R.layout.") || !viewIdentifier.contains("."))
newView = replaceByLayout(context, view, viewIdentifier);
else
newView = replaceByViewClass(context, view, viewIdentifier);
if (newView != null) {
ViewGroup parent = (ViewGroup) view.getParent();
newView.setLayoutParams(view.getLayoutParams());
newView.setId(view.getId());
newView.setTag(CarpaccioHelper.removeTag(view, tagToRemove));
if (view.getBackground() != null)
newView.setBackgroundDrawable(view.getBackground());
if (view instanceof ImageView && newView instanceof ImageView) {
ImageView imgView = (ImageView) view;
ImageView imgNewView = (ImageView) newView;
if (imgView.getDrawable() != null)
imgNewView.setImageDrawable(imgView.getDrawable());
}
if (view instanceof TextView && newView instanceof TextView) {
TextView txtView = (TextView) view;
TextView txtNewView = (TextView) newView;
if (txtView.getText() != null)
txtNewView.setText(txtView.getText());
if (txtView.getHint() != null)
txtNewView.setHint(txtView.getText());
}
int index = parent.indexOfChild(view);
parent.addView(newView, index);
parent.removeView(view);
}
Carpaccio carpaccio = CarpaccioHelper.registerToParentCarpaccio(newView);
if(carpaccio != null){
carpaccio.getCarpaccioManager().executeActionsOnView(newView);
}
return (T) newView;
}
/**
* If a view hasn't had an id set we need to set a temporary one in order to save state, since a
* view won't save its state unless it has an id. The view's id is also the key into the sparse
* array for its saved state, so the temporary one we choose just needs to be consistent between
* saving and restoring state.
*/
private void setIdIfNoneExists(View view) {
if (view.getId() == View.NO_ID) {
view.setId(R.id.view_model_state_saving_id);
}
}