简体   繁体   English

如何在android中避免相对布局中的重叠视图?

[英]How to avoid overlap view in relative layout in 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

The above layout is a page that divide horizitonally, for the left side , there is a list of textview and button , for the right side, there is an image view. 上面的布局是一个划分水平的页面,左边是一个textview和button的列表,右边是一个图像视图。 The problem is: when the textview content is too long, the imageview will overlap the content of it, besides using bringtofront(), are there any way (in xml ) to resize the width of the text view if it overlap with image view? 问题是:当textview内容太长时,imageview会重叠它的内容,除了使用bringtofront()之外,如果它与图像视图重叠,有没有办法(在xml中)调整文本视图的宽度?

Use layout_toStartOf in the first item with second item +id under double quotes 在第一个项目中使用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>

note this argument in textview 在textview中注意这个参数

android:layout_toStartOf="@+id/selectaccount"

An XML is read from top to bottom 从上到下读取XML

so this is how the Layout is rendered in Android 这就是在Android中呈现布局的方式

  • android:layout_toStartOf=" @id/item means that item is defined above this line 机器人:layout_toStartOf =” @ ID /项目意味着 高于该线限定
  • android:layout_toStartOf=" @+id/item means that item will appear later somewhere below this line android:layout_toStartOf =“ @ + id / item表示该项目此行下方某处出现

If you are using this kind of design than you should use linear Layout. 如果您使用这种设计,则应使用线性布局。 and use Table Rows in it to display this kind of view. 并在其中使用表行来显示此类视图。

and also use weight so that your view doesn't et overlap on other views. 并且还使用权重,以便您的视图不会与其他视图重叠。 try like this: 试试这样:

<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>

Hope it Helps!! 希望能帮助到你!!

将所有textviews与leftOf imageview对齐,如lastone: android:layout_toLeftOf="@id/imageView1" imageView1 android:layout_toLeftOf="@id/imageView1"

<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>

I had a similar problem and tried pretty much everything suggested here and in other similar threads. 我有一个类似的问题,并尝试了几乎所有在这里和其他类似的线程建议。

The one additional thing that did it for me was to add: 为我做的另一件事是添加:

android:layout_toStartOf="@+id/youroverlappeditem"

so the overlapping item will only run up to the overlapped item. 所以重叠项目只会运行到重叠项目。

layout_toLeftOf was not enough on its own layout_toLeftOf本身还不够

and of course ellipsize="end" was needed as appropriate. 当然还需要ellipsize="end"

Mine was for a recyclerview with a relative layout. 我的是一个具有相对布局的recyclerview

You can overcome that issue by clicking on "infer constraints". 您可以通过单击“推断约束”来克服该问题。 Follow the picture which will show the way: 按照将显示方式的图片:

怎么去AndroidStudio

Try constraint layout . 尝试约束布局 It simplifies your layout. 它简化了您的布局。

Add this line one your xml layout 在xml布局中添加此行

tools:ignore="RelativeOverlap" 工具:忽略= “RelativeOverlap”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM