gpt4 book ai didi

java - 如何在创建 View 时测量 View 的宽度?

转载 作者:行者123 更新时间:2023-11-30 11:02:39 26 4
gpt4 key购买 nike

我正在尝试连接单个单词的 TextViews 并创建它们的多行。为了确定何时创建新行,我需要检查单词的 TextView 的宽度。

这是一张类似于我想要完成的图片

preview picture

当我尝试使用 getWidth 时出现奇怪的错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object reference

但是,当我调试代码时,rL2 不为空并且有一个值。

我猜我的 View 还没有创建?但是我怎样才能动态地改变它呢?我怎样才能做我的行?

这是我的代码:

@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;
}

最佳答案

How can I measure the width of my views as I create them?

你不能。这是在测量和布局过程中处理的,这发生在您创建小部件之后。

I'm trying to connect TextViews of single words and create multilpe rows of them.

看起来你应该使用 a FlowLayout ,或类似的东西,作为 TextViews 的容器,它将自动包装它们。

如果 FlowLayout 不能满足您的需求,here is another one .

而且,如果这些都不是您想要的,您可以使用它们作为灵感来编写您自己的。您尝试实现的 UI 规则应该在容器中实现,因为容器会处理其子项的大小和定位。

关于java - 如何在创建 View 时测量 View 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com