繁体   English   中英

如何确保Relativelayout中两个视图之间的最小间距

[英]how to ensure the min space between two views in Relativelayout

我在RelativeLayout中有两个TextViews。 一个在左边,另一个在右边。 它们都显示一个字符串,该字符串可能会变得很长。 我想在两个TextView之间留出10dp的最小空间,如果没有足够的空间,让右边的一个椭圆。

我有下面的布局定义不起作用。 如果它很孤独,则正确的TextView可能会覆盖第一个。

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_marginLeft="3dp">
        <TextView
            android:id="@+id/left_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="7dp"
            android:gravity="bottom"
            android:text=""/>

        <TextView
            android:id="@+id/right_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:maxLines="1"
            android:ellipsize="end"
            />



</RelativeLayout>

我唯一可以做的方法是将最大长度设置为正确的长度。 但这不是我想要的,因为如果左边的一个短,我希望右边的一个比maxWidth长。

当我添加“ toRightOf”时,此功能但是textView的宽度对于其中显示的短字符串来说太长了。 那是不可接受的

在right_one文本Add android:layout_toRightOf="@+id/left_oneAdd android:layout_toRightOf="@+id/left_one

从right_one textview中删除android:layout_alignParentRight="true"

在right_one textview android:layout_marginLeft="10dp"替换为android:layout_marginRight="10dp"

为了使right_one textview保持正确,如果较短,则将right_one textview部分替换为

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="10dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/left_one>
     <TextView
            android:id="@+id/right_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:maxLines="1"
            android:ellipsize="end"
        />
</RelativeLayout>

加:

android:layout_toLeftOf="@+id/right_one"
android:layout_marginRight="10dp"

到您的left_one TextView

你可以这样

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


   <TextView
       android:id="@+id/left_one"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_marginBottom="7dp"
       android:gravity="bottom"
       android:layout_weight="0.3"
       android:text="Left text "/>

   <TextView
       android:id="@+id/right_one"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentRight="true"
       android:gravity="center"
       android:maxLines="1"
       android:ellipsize="end"
       android:layout_weight="0.7"
       android:layout_marginLeft="10dp"
       android:text="edkjfcnrdskjvkjrdfvsxsdcndsjkvnjksdnvkj 
        sdjkvbksdbvkj zskjvn jsdnbjkvfdbde"/>

  </LinearLayout>

暂无
暂无

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

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