2016-05-28 2 views
1

Моя проблема заключается в том, что когда я пытаюсь всегда ставить ImageView в том же макете, что и TextView, и когда SetText(); записывает текст в текстовое изображение, ImageView выходит за рамки макета. Да, я знаю, что я мог бы поставить изображение на задний план, но мое изображение устанавливается setImageResource, и я не могу изменить его или проект идет вниз ...Помещение ImageView рядом с TextView

Так вот мой XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="onhar.personalhealth.Main" 
    android:baselineAligned="false" 
    android:weightSum="1" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginBottom="100dp" 
     android:id="@+id/tip" 
     android:text="" 
     android:layout_gravity="bottom" /> 

    <ImageView 
     android:layout_width="260dp" 
     android:layout_height="260dp" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:id="@+id/imageView2" /> 

</LinearLayout> 

Спасибо за то, кто отвечает на это!

+1

почему вы даете жёстко прописанные ширину textView.Change его wrap_content и попробовать. –

ответ

1

Используйте layout_weight атрибут:

 <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginBottom="100dp" 
     android:id="@+id/tip" 
     android:text="" 
     android:layout_gravity="bottom" /> 


    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:id="@+id/imageView2" /> 
1

Использование android:layout_weight

Попробуйте

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="onhar.personalhealth.Main" 
    android:baselineAligned="false" 
    android:weightSum="1" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginBottom="100dp" 
     android:id="@+id/tip" 
     android:text="" 
     android:layout_gravity="bottom" /> 

    <ImageView 
     android:layout_width="260dp" 
     android:layout_height="260dp" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:id="@+id/imageView2" /> 

</LinearLayout> 
0

Кажется, вы используете фиксированные ширины для обоих представлений. Итак, вы можете использовать FrameLayout, чтобы держать их близко друг к другу. Таким образом, всякий раз, когда переполняется TextView, он будет находиться ниже ImageView.

<FrameLayout 
    android:layout_width="560dp" 
    android:layout_height="..."> 

    <TextView 
     android:layout_width="260dp" 
     android:layout_height="..." 
     android:layout_gravity="left"/> 

    <ImageView 
     android:layout_width="300dp" 
     android:layout_height="..." 
     android:layout_gravity="right"/> 

</FrameLayout> 

Другой вариант заключается в использовании динамической ширины для ImageView. Таким образом, размер ImageView будет изменен на основе оставшегося пространства в этом LinearLayout.

1

Добавить эту строку в текст. Это добавит изображение левой стороны TextViewandroid:drawableLeft="@drawable/YOUR_IMAGE"

Вот полный XML:

<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:drawableLeft="@drawable/YOUR_IMAGE" 
     android:gravity="center_vertical" 
     android:textStyle="bold" 
     android:textSize="24dip" 
     android:maxLines="1" 
     android:ellipsize="end"/> 
0

Для отзывчивого интерфейса необходимо использовать относительный макет, он будет масштабироваться и выглядеть одинаково во всех устройствах этого, как вы могли бы добиться этого.

<TextView 
    android:id="@+id/tip" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toLeftOf="@+id/imageView2" 
    android:text="some \n really \n realllllyyyyy \n \n long \n text a \n who \n just \n do not \n\n\n\ fit " 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="260dp" 
    android:layout_height="260dp" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/ic_launcher" /> 

Результат

Независимо от длины текста изображение будет всегда будет на правой стороне с размером 260dp & текст будет масштабироваться в зависимости от ширины экран устройства и ничего не выйдет из экрана.

enter image description here

Смежные вопросы