Я пытаюсь разбить предложение на слова и поместить слова в RelativeLayout как TextViews, но когда я устанавливаю layoutparams на TextView, это не действует в API 23 и старше. Он отлично работает с API 24 и новее, только если я использую LinearLayout.LayoutParams вместо RelativeLayout.LayoutParams.
Это мой метод разделения:
public void splitWords(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = 0;
int bgId = 0;
leftM = convertToDp(5);
String [] splitSentence = sentence.split(" ");
size = splitSentence.length;
bgId = R.drawable.word_bg;
for (int i = 0;i < size;i++){
final TextView word = new TextView(this);
word.setText(splitSentence[i]);
word.setBackgroundResource(bgId);
word.measure(0, 0);
int butonWidth = word.getMeasuredWidth();
if(width - convertToDp(60) - leftM <= butonWidth){
leftM = convertToDp(5);
topM += butonWidth + 5;
}
params.leftMargin = leftM + 2;
params.topMargin = topM;
word.setLayoutParams(params);
leftM += butonWidth;
wordsContainer.addView(word);
}
wordsContainer.setGravity (Gravity.CENTER) ; }
А это мой RelativeLayout:
<RelativeLayout
android:id="@+id/wordsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="20dp"
android:layout_marginRight="2dp"
android:background="@drawable/white_bg_no_border"
android:elevation="2dp"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:paddingRight="20dp"
android:paddingBottom="50dp" />