Tôi có một phần bố cục tương đối. Trong đó, tôi phải lập trình thêm một số bản xem trước văn bản có thể nhấp có thể thay đổi về độ rộng. Tôi cần chúng xuất hiện cái khác. Nếu một textView có chiều rộng sao cho nó không thể vừa với không gian còn lại của hàng hiện tại, nó sẽ bắt đầu xuất hiện bên dưới hàng theo kiểu tương tự.Thêm một số textViews (có chiều rộng thay đổi) trong relativeLayout cái khác
tôi cần một cái gì đó như thế này:
gì tôi đã được quản lý cho đến nay là mã này dưới đây:
public static void updateWordsListingOnUi(final ArrayList<String> wordsList) { //TODO: incomplete and may be inefficient
mUiHandler.post(new Runnable() {
@Override
public void run() {
TextView textView;
wordsListingContainer.removeAllViewsInLayout(); //TODO
for (final String word : wordsList)
{
textView = new TextView(context);
textView.setText(word);
textView.setClickable(true);
textView.setPadding(1,1,10,1);
if (wordsList.indexOf(word)%2 == 0) {
textView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
}
else {
textView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
textView.setId(wordsList.indexOf(word)); //give IDs from 0 - (maxSize-1)
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, word, Toast.LENGTH_SHORT).show();
}
});
if(wordsList.indexOf(word) > 0) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.RIGHT_OF, textView.getId()-1); //TODO
wordsListingContainer.addView(textView,layoutParams);
}
else {
wordsListingContainer.addView(textView);
}
}
}
});
}
này không được công việc một phần. Nó đặt textviews cái này đến cái kia, nhưng khi có đủ không gian, nó sẽ không được xếp vào hàng kế tiếp. Tôi không thể nghĩ làm thế nào tôi có thể làm điều đó.
đang có một số thư viện có thể giúp bạn với điều đó. Hầu hết sử dụng 1 chế độ xem văn bản tùy chỉnh trong đó u có thể "bọc bong bóng" các mục có thể nhấp riêng lẻ. Về điều đó, tôi nghĩ đây là https://github.com/eyeem/chips-android nhưng bạn có thể chắc chắn rằng Google sẽ có nhiều hơn thế. – Budius