Java 如何在创建视图时测量视图的宽度?
我正在尝试连接单个单词的Java 如何在创建视图时测量视图的宽度?,java,android,Java,Android,我正在尝试连接单个单词的textview,并创建它们的多行。为了确定何时创建新行,我需要检查单词文本视图的宽度 这是一张与我想完成的任务相似的图片 当我尝试使用getWidth时,出现了一个奇怪的错误: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object referen
textview
,并创建它们的多行。为了确定何时创建新行,我需要检查单词文本视图的宽度
这是一张与我想完成的任务相似的图片
当我尝试使用getWidth
时,出现了一个奇怪的错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object reference
然而,当我调试代码rL2时,它不是空的,并且有一个值
我猜我的视图还没有创建?但是我如何动态地改变它呢?我怎样才能排好队
这是我的密码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_call_response, container, false);
v.measure(widthMeasureSpec, heightMeasureSpec);
int lineWidth = 0;
RelativeLayout rL2 = (RelativeLayout) v.findViewById(R.id.row1);
int lineCount = 0;
int rowWidth = rL2.getWidth();
int wordCountRow = 0;
TextView question = (TextView) v.findViewById(R.id.Question);
TextView choice1 = (TextView) v.findViewById(R.id.choice1);
TextView choice2 = (TextView) v.findViewById(R.id.choice2);
TextView choice3 = (TextView) v.findViewById(R.id.choice3);
TextView choice4 = (TextView) v.findViewById(R.id.choice4);
TextView speaker = (TextView) v.findViewById(R.id.Speaker);
TextView progress = (TextView) v.findViewById(R.id.progress);
ArrayList<String> answers = new ArrayList<>();
answers.addAll(quote.getAnswerChoice());
answers.add(quote.getWordSplitTypeA().get(quote.getAnswerIndex().get(0)));
Collections.shuffle(answers);
speaker.setText("How would " + quote.getSpeaker().get(0) + " Marx answer the above question?");
choice1.setText(answers.get(0));
choice2.setText(answers.get(1));
choice3.setText(answers.get(2));
choice4.setText(answers.get(3));
progress.setText(String.format("%.1f%% Complete", (SingletonFactory.getPlayer().getLessonProgress()[0] / 12.0) * 100));
//addTextViews(v);
for(int i = 0;i<quote.getWordSplitTypeA().size(); i++) {
if (lineWidth + 10 > rowWidth) {
lineCount++;
wordCountRow = 0;
}
int padding = getResources().getDimensionPixelOffset(R.dimen.wordBoxPadding);
TextView tv = new TextView(getActivity());
tv.setId(i);
tv.setText(quote.getWordSplitTypeA().get(i));
tv.setBackgroundColor(Color.parseColor(bGColor));
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setPadding(padding, padding, padding, padding);
ViewGroup.LayoutParams LP = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(LP);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
if (wordCountRow == 0) {
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
} else {
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.RIGHT_OF, (i - 1));
}
tv.setLayoutParams(params);
if (quote.getAnswerIndex().contains(i)) {
tv.setBackgroundColor(Color.RED);
tv.setText(".....");
}
int width = tv.getWidth();
lineWidth += width;
if (lineCount == 0) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row1);
rL.addView(tv);
} else if (lineCount == 1) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row2);
rL.addView(tv);
} else if (lineCount == 2) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row3);
rL.addView(tv);
} else if (lineCount == 3) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row4);
rL.addView(tv);
}
choice1.setOnClickListener(mClickListener);
choice2.setOnClickListener(mClickListener);
choice3.setOnClickListener(mClickListener);
choice4.setOnClickListener(mClickListener);
question.setText(quote.getSpeaker().get(0) + ": " + quote.getQuote().get(0));
}
return v;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图v=充气机。充气(R.layout.fragment\u call\u response,container,false);
v、 测量(宽度测量、高度测量);
int线宽=0;
RelativeLayout rL2=(RelativeLayout)v.findViewById(R.id.row1);
int lineCount=0;
int rowWidth=rL2.getWidth();
int wordCountRow=0;
TextView问题=(TextView)v.findViewById(R.id.question);
TextView选项1=(TextView)v.findViewById(R.id.choice1);
TextView选项2=(TextView)v.findViewById(R.id.choice2);
TextView选项3=(TextView)v.findViewById(R.id.choice3);
TextView选项4=(TextView)v.findViewById(R.id.choice4);
TextView扬声器=(TextView)v.findviewbyd(R.id.speaker);
TextView进度=(TextView)v.findViewById(R.id.progress);
ArrayList answers=新的ArrayList();
answers.addAll(quote.getAnswerChoice());
add(quote.getWordSplitTypeA().get(quote.getAnswerIndex().get(0));
集合。洗牌(答案);
speaker.setText(“如何“+引用.getSpeaker().get(0)+”马克思回答上述问题?”);
choice1.setText(answers.get(0));
选择2.setText(answers.get(1));
选择3.setText(answers.get(2));
选择4.setText(answers.get(3));
progress.setText(String.format(“%.1f%%Complete”,(SingletonFactory.getPlayer().getLessonProgress()[0]/12.0)*100));
//添加文本视图(v);
对于(int i=0;i行宽度){
lineCount++;
wordCountRow=0;
}
int padding=getResources().getDimensionPixelOffset(R.dimen.wordBoxPadding);
TextView tv=新的TextView(getActivity());
tv.setId(i);
tv.setText(quote.getWordSplitTypeA().get(i));
tv.setBackgroundColor(Color.parseColor(bGColor));
电视.彩色(彩色.白色);
电视。设置重力(重力。重心);
设置填充(填充,填充,填充,填充);
ViewGroup.LayoutParams LP=新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容);
tv.setLayoutParams(LP);
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)tv.getLayoutParams();
如果(wordCountRow==0){
参数addRule(相对中心垂直);
params.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT);
params.addRule(RelativeLayout.ALIGN\u PARENT\u START);
}否则{
参数addRule(相对中心垂直);
参数addRule(RelativeLayout.RIGHT_OF,(i-1));
}
tv.setLayoutParams(参数);
if(quote.getAnswerIndex()包含(i)){
tv.setBackgroundColor(Color.RED);
tv.setText(“…”);
}
int width=tv.getWidth();
线宽+=宽度;
如果(行数==0){
RelativeLayout rL=(RelativeLayout)v.findViewById(R.id.row1);
rL.addView(电视);
}else if(lineCount==1){
RelativeLayout rL=(RelativeLayout)v.findViewById(R.id.row2);
rL.addView(电视);
}else if(行数==2){
RelativeLayout rL=(RelativeLayout)v.findViewById(R.id.row3);
rL.addView(电视);
}else if(行数==3){
RelativeLayout rL=(RelativeLayout)v.findViewById(R.id.row4);
rL.addView(电视);
}
选项1.setOnClickListener(McClickListener);
选择2.setOnClickListener(McClickListener);
选项3.setOnClickListener(McClickListener);
选择4.setOnClickListener(McClickListener);
question.setText(quote.getSpeaker().get(0)+:“+quote.getQuote().get(0));
}
返回v;
}
如何在创建视图时测量视图的宽度
你不能。这是在测量和布局过程中处理的,这在创建小部件之后很好地发生
我正在尝试连接单个单词的文本视图并创建它们的多行
看起来您应该使用或类似的东西作为文本视图
的容器,它将自动包装它们
如果FlowLayout
不能满足您的需要
而且,如果两者都不是你想要的,你可以用它们作为你自己写作的灵感。您试图实现的UI规则应该在容器中实现,因为容器处理其子项的大小和定位。感谢第二个链接起作用,但是第一个链接出现错误“java.lang.ClassCastException:android.view.ViewGroup$LayoutParams不能强制转换为android.view.ViewGroup$MarginLayoutParams”与完全相同的代码,所以我建议第二个链接