两个视图相互引用

时间:2014-07-11 09:15:58

标签: android android-layout android-xml

我有两个文本视图声明如下。我的目的是并排显示两个视图,其中左视图(显示用户名)具有更大的文本大小,右视图显示最后一个消息发送的时间,并且应始终可见(即使用户名字很长,这就是为什么我' m使用android:layout_toLeftOf)。但是,左视图较小,我想将其基线与右视图对齐。这是非常好的依赖在哪里,我无法解决它。

部分可接受的解决方案是在右视图中使用“android:layout_toRightOf”,但如果用户的名字很长,那么时间(右视图)将被椭圆化(它在AppTheme.TextView.SingleLine中声明)。

所以基本上,我的问题是,两个视图是否可以相互引用?我明白为什么我会收到这个错误,但是我无法解决它。 我记得在我的C / C ++时代,可以在文件的顶部声明函数,然后在其他地方定义它(因此编译器不会抱怨),我认为这是我需要的东西。

    <TextView
        android:id="@+id/fragment_messages_item_sender_name"
        style="@style/AppTheme.TextView.SingleLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/fragment_messages_item_last_msg_time"
        android:textSize="@dimen/global_text_large"/>

    <TextView
        android:id="@+id/fragment_messages_item_last_msg_time"
        style="@style/AppTheme.TextView.SingleLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/fragment_messages_item_sender_name"
        android:layout_alignParentEnd="true"
        android:gravity="right"/>

我所得到的只是

Error:(27, 38) No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/fragment_messages_item_last_msg_time').

3 个答案:

答案 0 :(得分:1)

在R class id中

fragment_messages_item_last_msg_time

不存在 为避免该问题需要在具有id关系的声明字段之前使用“+”

android:layout_toLeftOf="+@id/fragment_messages_item_last_msg_time"

答案 1 :(得分:0)

您可以在另一个android:layout_alignBaseline中移动TextView。注意id循环,通常会产生令人讨厌的崩溃。

关于您的问题,您必须记住R中的条目标记为public static final,并且+为指定ID生成新条目,如果它不存在,那么您可以有:

android:layout_toLeftOf="@+id/fragment_messages_item_last_msg_time"

在第一个TextView

android:id="@id/fragment_messages_item_last_msg_time"

到第二个。正如我之前提到的,不允许在RelativeLayout中使用循环,这些将使您的应用程序崩溃

答案 2 :(得分:0)

Hi use below code :    

<LinearLayout 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="horizontal" >

        <TextView
            android:id="@+id/fragment_messages_item_sender_name"
            style="@style/AppTheme.TextView.SingleLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Manish" />

        <TextView
            android:id="@+id/fragment_messages_item_last_msg_time"
            style="@style/AppTheme.TextView.SingleLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Khamar" />

    </LinearLayout>
相关问题