2

Получаю данные и добавляю TextView в цикле, указать внутренний отступ могу, а вот с внешним отступом проблемка))

Вот код как я делаю:

for (int i = 0; i < response.body().getInterests().getInterests().size(); i++) {
    com.google.android.flexbox.FlexboxLayout interestsLayout = (com.google.android.flexbox.FlexboxLayout) findViewById(R.id.interestsLayout);

    TextView interestsTextView = new TextView(UserPageActivity.this);
    interestsTextView.setText(userData.getInterests().getInterests().get(i).toString());
    interestsTextView.setBackgroundResource(R.drawable.circle_button_gray);
    interestsTextView.setPadding(10,10,10,10);

    LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textViewLayoutParams.setMargins(8,8,8,8);
    interestsTextView.setLayoutParams(textViewLayoutParams);

    interestsLayout.addView(interestsTextView); 
} 

Спасибо!

2
  • 1
    textViewLayoutParams.setMargins(16,16,16,16);
    – McDaggen
    5 авг 2018 в 9:17
  • Спасибо большое)), не сработало, расстояние не меняется... что-то не то
    – dev
    5 авг 2018 в 9:19

2 ответа 2

0

Задавайте внешний отступ через ваш LinearLayout.LayoutParams

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView interestsTextView = new TextView(UserPageActivity.this);

interestsTextView.setText(userData.getInterests().getInterests().get(i).toString());
interestsTextView.setBackgroundResource(R.drawable.circle_button_gray);
interestsTextView.setPadding(10,10,10,10);
params.setMargins(8,8,8,8);
interestsTextView.setLayoutParams(params);

interestsLayout.addView(interestsTextView);
10
  • В вопросе так и есть
    – dev
    5 авг 2018 в 9:25
  • Все что идёт в LinearLayout.LayoutParams поднимите выше TextView. То есть делайте это до объявления TextView
    – McDaggen
    5 авг 2018 в 9:28
  • Ничего не изменилось
    – dev
    5 авг 2018 в 9:31
  • LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) interestsTextView.getLayoutParams(); замените, и теперь точно должно сработать
    – McDaggen
    5 авг 2018 в 9:40
  • Не совсем понял, на что нужно поменять?
    – dev
    5 авг 2018 в 13:19
0

Решил вопрос, простым добавлением пустого компонента с немного увеличенным размером, т.е. установил те отступы которые мне нужны)). Возможно это не правильно, но это работает и другого выхода я не нашел.

for (int i = 0; i < response.body().getInterests().getInterests().size(); i++) {
    com.google.android.flexbox.FlexboxLayout interestsLayout = (com.google.android.flexbox.FlexboxLayout) findViewById(R.id.interestsLayout);

    TextView interestsTextView = new TextView(UserPageActivity.this);
    interestsTextView.setText(userData.getInterests().getInterests().get(i).toString());
    interestsTextView.setBackgroundResource(R.drawable.circle_button_gray);
    interestsTextView.setPadding(10,10,10,10);

    LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textViewLayoutParams.setMargins(8,8,8,8);
    interestsTextView.setLayoutParams(textViewLayoutParams);
    interestsLayout.addView(interestsTextView); 

    TextView nullTextView = new TextView(UserPageActivity.this);
    nullTextView.setText("");
    nullTextView.setPadding(5,10,5,20);
    nullTextView.setLayoutParams(params);
    interestsLayout.addView(nullTextView);
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.