Подтвердить что ты не робот

Как избежать отображения перекрытия в относительной компоновке в android?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView3"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView4"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView5"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView6"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView7"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView8"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView9" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商店圖片:"
        android:textSize="15sp"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@id/imageView1" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textView10"
        android:contentDescription="@string/top" />
</RelativeLayout>

Simple output:
textview1     textview9
textview2     imageview1
.
.
.
button1

Вышеупомянутая компоновка - это страница, разделяющая горизонтально, для левой стороны есть список текстовых просмотров и кнопка, для правой стороны - изображение. Проблема заключается в следующем: когда содержимое текста слишком длинное, изображение будет перекрывать его содержимое, кроме того, используя метод usetopront(), есть ли способ (в xml) изменить размер текстового представления, если он будет перекрываться с представлением изображения?

4b9b3361

Ответ 1

Если вы используете такой дизайн, вы должны использовать линейный макет. и используйте Табличные строки в нем, чтобы отобразить этот вид.

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<TableRow 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:weightSum="10">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textSize="30sp"
        android:text="test"
        android:layout_weight="5" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:textSize="20sp" 
        android:text="test"
        android:layout_weight="5"/>
</TableRow>

</LinearLayout>
</ScrollView>

Надеюсь, что это поможет!

Ответ 2

Используйте layout_toStartOf в первом элементе со вторым элементом + id в двойных кавычках

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/selectaccount"
        android:text="very long text which used to overlap over radio button"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioButton
        android:id="@+id/selectaccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

отметить этот аргумент в текстовом виде

android:layout_toStartOf="@+id/selectaccount"

XML читается сверху вниз

так вот как выглядит макет в Android

  • android: layout_toStartOf = " @id/item означает, что элемент определен выше. Эта строка
  • android: layout_toStartOf = " @+ id/item означает, что элемент появится ниже где-нибудь ниже этой строки

Ответ 3

Выровняйте все текстовые изображения влево, как показано на рисунке: android:layout_toLeftOf="@id/imageView1"

Ответ 4

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1.0" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight=".5"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
         />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight=".5"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

Ответ 5

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

Еще одна вещь, которая сделала это для меня, заключалась в том, чтобы добавить:

android:layout_toStartOf="@+id/youroverlappeditem"

чтобы перекрывающийся элемент работал только с перекрывающимся элементом.

layout_toLeftOf не было само по себе

и, конечно, ellipsize="end" был необходим по мере необходимости.

Шахта была для recyclerview с относительной компоновкой.

Ответ 6

Вы можете преодолеть эту проблему, нажав на "infer constraints". Следуйте рисунку, который покажет путь:

HowTo AndroidStudio

Ответ 8

Добавьте эту строку в один xml-макет

инструменты: игнорировать = "RelativeOverlap"