0
public RelativeLayout textSender(String text, String citation, String photoURI, String photoDesc, int margin, RelativeLayout prevRelative, int iterator) {
    int containerId = 100 + iterator;
    int textId = 200 + iterator;
    int citationId = 300 + iterator;
    int photoId = 400 + iterator;
    int photoDescId = 500 + iterator;
    int staples = 1000 + iterator;
    //int photoDescContainerId = 400 + iterator;

    //Конейнер всього блоку
    final RelativeLayout relative = new RelativeLayout(activity);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, (int) convertDpToPixel(margin), 0, 0);
    layoutParams.addRule(RelativeLayout.BELOW, prevRelative.getId());
    relative.setLayoutParams(layoutParams);
    relative.setId(containerId);

    //Текстове поле опису новини
    final TextView textView = new TextView(activity);
    RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    textParams.setMargins(0, 0, 0, 0);
    textView.setLayoutParams(textParams);
    textView.setId(textId);
    textView.setGravity(Gravity.LEFT);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
    textView.setTextColor(Color.BLACK);
    if (!text.equals("")) {
        textView.setText(Html.fromHtml(text));
    }else{
        textView.setVisibility(TextView.GONE);
    }

    //Лапки
    final ImageView staplesImage = new ImageView(activity);
    RelativeLayout.LayoutParams staplesImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    staplesImageParams.setMargins(0, 0, 0, 0);
    staplesImageParams.addRule(RelativeLayout.BELOW, textView.getId());
    staplesImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    staplesImage.setLayoutParams(staplesImageParams);
    staplesImage.setId(staples);
    staplesImage.setImageResource(R.mipmap.staples);
    //Цитата
    final TextView citationTextView = new TextView(activity);
    RelativeLayout.LayoutParams citationParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    citationParams.setMargins(0, 0, 0, 0);
    citationParams.addRule(RelativeLayout.BELOW, textView.getId());
    citationParams.addRule(RelativeLayout.RIGHT_OF, staplesImage.getId());
    citationTextView.setLayoutParams(citationParams);
    citationTextView.setId(citationId);
    citationTextView.setGravity(Gravity.CENTER);
    citationTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
    citationTextView.setTextColor(Color.GRAY);
    if (!citation.equals("")) {
        citationTextView.setText(Html.fromHtml(text));
    }else{
        citationTextView.setVisibility(TextView.GONE);
        staplesImage.setVisibility(ImageView.GONE);
    }
    //--

    //Зображення
    final ImageView photoImage = new ImageView(activity);
    RelativeLayout.LayoutParams photoParams = new RelativeLayout.LayoutParams( (int)convertDpToPixel(250), (int)convertDpToPixel(250));
    photoParams.setMargins(0, 0, 0, 0);
    photoParams.addRule(RelativeLayout.BELOW, citationTextView.getId());
    photoParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    photoImage.setLayoutParams(photoParams);
    photoImage.setId(photoId);
    photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
    if (!photoURI.equals("")) {
        ImageLoader loader = new ImageLoader(photoURI, photoImage);
        loader.execute();
        Bitmap bmp = null;
        try {
            bmp = loader.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        photoImage.setImageBitmap(bmp);
    }else{
        photoImage.setVisibility(ImageView.GONE);
    }

    relative.addView(textView);
    relative.addView(staplesImage);
    relative.addView(citationTextView);
    relative.addView(photoImage);
    listContainer.addView(relative);
    //--
    return relative;
}

Помогите пожалуйста! У меня есть ImageView(staplesImage) и TextView(citationTextView), мне нужно сделать так, что бы ImageView был всегда по центру относительно TextView, что бы было вот так:

введите сюда описание изображения

Но без добавления еще одного Layout.

1
  • обязательно это делать программно? 23 ноя 2015 в 17:05

2 ответа 2

1
staplesImageParams.addRule(RelativeLayout.ALIGN_BOTTOM, citationTextView.getId()); 
staplesImageParams.addRule(RelativeLayout.ALIGN_TOP, citationTextView.getId()); 
staplesImageParams.addRule(RelativeLayout.LEFT_OF, citationTextView.getId()); 
staplesImage.setGravity(Gravity.CENTER)
0

Вам, скорее всего, нужно setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom):

mTextView.setCompoundDrawablesWithIntrinsicBounds(ТУТ_ССЫЛКА_НА_ЭКЗЕМПЛЯР_DRAWABLE, null, null, null);
0

Ваш ответ

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

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