java - Android:为什么我的第二个 TextView 不显示,而第一个 TextView 却显示?

标签 java android

我添加了第二个 TextView ,它应该位于第一个 TextView 下方,但现在第一个 TextView 正在工作,但第二个没有出现。

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/state1"
android:textSize="16sp" />

<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@id/textView1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer1"
android:textSize="12sp" />


<TextView
android:id="@+id/thirdLine"
android:layout_below="@+id/textView2" 
android:text="@string/state2"
android:textSize="16sp" />

<TextView
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" 
android:text="@string/sayer2"
android:textSize="16sp" />

</RelativeLayout>

最佳答案

您需要三件事才能让其他人出现。

第一:

将此属性 android:layout_height="?android:attr/listPreferredItemHeight" 更改为 android:width="match_parent"

第二个:

为您的 TextView 设置宽度/高度。 thirdLinetextView2 没有合适的宽度/高度。尝试这样做:

android:layout_width="match_parent"
android:layout_height="wrap_content"  

最后:

不要尝试将 TextView 放置在另一个其对齐方式为 alignParentBottom 的下方。相反,尝试添加一个容器,其对齐方式位于全局父级的底部。见下文:

尝试这种格式:

<?xml version="1.0" encoding="utf-8"?>
// set a height to wrap_content/match_parent/fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dip" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="state1"
        android:textSize="16sp" />
        
    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_below="@id/textView1"
        android:ellipsize="end"
        android:singleLine="true"
        android:lines="1"
        android:text="sayer1"
        android:textSize="12sp" />
    
    // if you want to align the latter text
    // to the bottom, make a container as...
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
        
        // declare the width/height of the TextView
        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:text="sayer2"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/thirdLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_below="@+id/textView2" 
            android:text="state2"
            android:textSize="16sp" />

    </RelativeLayout>

</RelativeLayout>  

请告诉我这是否是预期的结果。希望这会有所帮助。

关于java - Android:为什么我的第二个 TextView 不显示,而第一个 TextView 却显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662699/

相关文章:

java - jar读取文件时出现空点异常

java - 在准备好的语句中,setString()是防止SQL注入(inject)的唯一有用方法吗?

java - 从无限循环中获取值

java - 使用公共(public)配置自动重新加载属性文件不起作用

java - Android JUnit 测试应用类

java - Java 项目的 Oracle 数据库连接不稳定

java - 通过自定义 ListView 从 Activity A 转到 Activity B

android - FirebaseNetworkException : A network error (such as timeout, 连接中断或无法访问主机)

android - 在 Android 上为 Deeplink 功能编写单元测试?

Android 嵌套布局