2
我試圖連接單個單詞的TextViews
並創建它們的多排行。爲了確定何時創建一個新行,我需要檢查單詞「TextViews」的寬度。我如何在創建視圖時測量視圖的寬度?
這裏是一個事先知情同意,類似於什麼即時試圖完成
我得到一個奇怪的錯誤,當我嘗試使用getWidth
:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object reference
然而,當我調試代碼rL2不是null並且有一個值。
我猜我的視圖尚未創建?但是我怎樣才能動態地改變它呢?我如何製作我的排?
這裏是我的代碼:
@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;
}
感謝第二個鏈接的工作,但第一個給出了一個錯誤「java.lang.ClassCastException:android.view.ViewGroup $ LayoutParams不能轉換爲android.view.ViewGroup $ MarginLayoutParams」與完全相同的代碼,所以ID建議秒鏈接 –