不显示TextView文本

时间:2019-03-02 17:01:31

标签: android android-layout

我正在使用相对布局来显示ImageView和TextView。但是不会显示分配给TextView的默认文本,请检查以下Xml代码:

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

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/background" />
    <RelativeLayout
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/trainName"
            android:textAlignment="center"
            android:background="#F81A07"
            android:textColor="#1BADEE"
            android:text="Default Text"
            android:textSize="20sp"
            android:padding="100dp"
            android:layout_alignParentBottom="true"/>


    </RelativeLayout>
</RelativeLayout>

5 个答案:

答案 0 :(得分:1)

在内部RelativeLayout中,您具有属性android:layout_height="100dp",该属性将使RelativeLayout的高度为100dp。

在该视图内的TextView内,您具有属性android:padding="100dp",该属性将在TextView的所有面上添加100dp填充,从而将其推到{的边界之外{1}}。

尝试调整内部RelativeLayout的高度减少TextView的填充。

答案 1 :(得分:0)

您应该被删除 android:layout_alignParentBottom="true”是TextView

答案 2 :(得分:0)

我尝试了您的代码,是的,您设置了错误的属性android:padding =“ 100dp”。

这是您的代码:

<TextView
            android:id="@+id/trainName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F81A07"
            android:text="Default Text"
            android:textAlignment="center"
            android:textColor="#1BADEE"
            android:textSize="20sp"
            android:visibility="visible" />

我同意蓬松机器人的回答。

快乐的加油!!

答案 3 :(得分:0)

我尝试了您的xml代码。有一个小问题 android:padding =“ 100dp”

从文本视图中删除它。请确保布局高度和子视图填充不相同。

这是我的xml代码,请尝试以下操作:

带填充

<RelativeLayout
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="100dp">


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/trainName"
android:text="Default Text"
android:textAlignment="center"
android:background="#F81A07"
android:textColor="#1BADEE"
android:textSize="20sp"
android:padding="40dp"/>
    </RelativeLayout>

无填充

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#ffffff">


    <TextView
        android:id="@+id/trainName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F81A07"
        android:text="Default Text"
        android:textAlignment="center"
        android:textColor="#1BADEE"
        android:textSize="20sp" />

</RelativeLayout>

希望它对您有用

答案 4 :(得分:0)

是的,蓬松的机器人的观点是正确的, 检查并更改。

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

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/background" />
    <RelativeLayout
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/trainName"
            android:textAlignment="center"
            android:background="#F81A07"
            android:textColor="#1BADEE"
            android:text="Default Text"
            android:textSize="20sp"
            android:padding="2dp"
            android:layout_alignParentBottom="true"/>


    </RelativeLayout>
</RelativeLayout>
相关问题