Thursday, January 23, 2014

Help, Fragment in how to get the width of the component



I want to be like QQ, QQ choose the kind of drop-down menu when you log on, but encountered a problem, I can not get the width of the component. Tried the whole life cycle, have access to the value 0. activity there onWindowFocusChanged () can get the width of the component lifecycle, Fragment there is no similar it?
Currently think of two ideas, one is to get the width of the component in the Fragment of a life cycle, the other one is not able to obtain Fragment assembly in activity, and then () to obtain its components in width Activity of onWindowFocusChanged.

Reply:
What you said is not Fragment components inside the child controls?
This is possible
Reply:
cited a floor ncepu307 reply:
what you said is not Fragment components inside the child controls?
This is possible

Yes, what I have in Fragment inside a EditText, click there will be a ListView, but now the ListView's width is determined by the EditText, it is necessary to get this job EditText width
Reply:
 private EditText mEditText; 

. . . .
mEditText = (EditText) rootView.findViewById (R.id.edittext);

. . . . .
int width = mEditText.getMeasuredWidth ();

Reply:
Direct access to the Activity inside Fragment elements inside, we want to help you, to help vote for it http://vote.blog.csdn.net/blogstaritem/blogstar2013/xiaanming
Reply:
references, 4th Floor xiaanming reply:
Fragment direct access to the inside of the element in the Activity inside, and I hope to help you, to help vote for it http://vote.blog.csdn.net/blogstaritem/blogstar2013/xiaanming

How to Get?
Reply:
reference to the third floor ncepu307 reply:
 private EditText mEditText; 

. . . .
mEditText = (EditText) rootView.findViewById (R.id.edittext);

. . . . .
int width = mEditText.getMeasuredWidth ();

Say, is the life cycle can not get, I do not know what the life cycle of the width of the sub Fragment able to get inside the assembly, this is a problem
Reply:
reference to the 6th floor MengHuanYuYe reply:
Quote: references to the third floor ncepu307 reply:

 private EditText mEditText; 

. . . .
mEditText = (EditText) rootView.findViewById (R.id.edittext);

. . . . .
int width = mEditText.getMeasuredWidth ();

Say, is the life cycle can not get, I do not know what the life cycle of the width of the sub Fragment able to get inside the assembly, this is a problem

Not right, you click on the EditText, go to execute the above code, you can certainly get to this width. Because this time has been calculated EditText finished size.

 mHandler.post (new Runnable () {
@ Override
public void run () {
int width = mEditText.getMeasuredWidth ();
}
}, 20);

Reply:
Activity layout with access to inside view as ah
Reply:
reference to the 7th floor ncepu307 reply:
Quote: references to the 6th floor MengHuanYuYe reply:

Quote: references to the third floor ncepu307 reply:

 private EditText mEditText; 

. . . .
mEditText = (EditText) rootView.findViewById (R.id.edittext);

. . . . .
int width = mEditText.getMeasuredWidth ();

Say, is the life cycle can not get, I do not know what the life cycle of the width of the sub Fragment able to get inside the assembly, this is a problem

Not right, you click on the EditText, go to execute the above code, you can certainly get to this width. Because this time has been calculated EditText finished size.

 mHandler.post (new Runnable () {
@ Override
public void run () {
int width = mEditText.getMeasuredWidth ();
}
}, 20);

Well, I have to say is not to get the width of this component immediately after clicking ...... and I know how to get the
ViewTreeObserver vto = parent1.getViewTreeObserver ();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {

@ Override
public void onGlobalLayout () {
/ / TODO Auto-generated method stub
pwidth1 = parent1.getWidth ();
}
});
So you can get the width of the component directly.

No comments:

Post a Comment