0
我的代碼:獲取TextView的大小編程
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Uri uri = Uri.parse("Home");
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.mesage_linear1);
final TextView message = new TextView(getActivity());
number_view.setText("Long Text");
int m_width = message.getMeasuredWidth();
int m_height = message.getMeasuredHeight();
Toast.makeText(getActivity(),"Height : "+m_height+"\nWidth : "+m_width,Toast.LENGTH_LONG).show();
return rootView;
}
我想要得到的編程方式創建TextView
的高度和寬度。
輸出爲高:0和寬度:0
加上'rootView' –
裏面你'TextiVew'你需要測量的高度和第一寬度。 message.measure(MeasureSpec.AT_MOST,MeasureSpec.AT_MOST); int m_width = message.getMeasuredWidth(); int m_height = message.getMeasuredHeight(); –